Jump to content

Dynamic image upload with incremental filenames


barrowvian

Recommended Posts

Basically, I have 3 files at the moment; the form, upload.php and a includes file.

 

The form;

<form action="./upload.php" method="post" enctype="multipart/form-data">
   <p>
      <label for="file">Select file 1:</label> <input type="file" name="profileimg" id="file"> <br />
      <label for="file">Select file 2:</label> <input type="file" name="userfile2" id="file"> <br />
      <label for="file">Select file 3:</label> <input type="file" name="userfile3" id="file"> <br />

      <button>Upload File</button>
   <p>
</form>

 

upload.php

<?php include("includes/header.php"); ?>

<?php
// Configuration of profile image
$filename = $_FILES['profileimg']['name']; // Get the name of the file (including file extension).

if ($filename == $_FILES['profileimg']['name']){

	$img = "profileimg";
	$imgname = "profile";
	include("includes/test2.php");
}
?>
  
<?php
// Configuration of profile image
$filename = $_FILES['userfile2']['name']; // Get the name of the file (including file extension).

if ($filename == $_FILES['userfile2']['name']){

	$img = "userfile2";
	$imgname = "userfile2";
	include("includes/test2.php");
}
?>

<?php
// Configuration of profile image
$filename = $_FILES['userfile3']['name']; // Get the name of the file (including file extension).

if ($filename == $_FILES['userfile3']['name']){

	$img = "userfile3";
	$imgname = "userfile3";
	include("includes/test2.php");
}
?>
<?php include("includes/footer.php"); ?>

 

and the includes file;

<?php
$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation. 
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = $_COOKIE['tmp']; // upload files to. called from cookie tmp from newuser.php
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
$pathinfo = pathinfo($_FILES["$img"]['name']); //gets the information about the file path???


   	// Check if the filetype is allowed, if not DIE and inform the user.
   	if(!in_array($ext,$allowed_filetypes))
    	die('The file you attempted to upload is not allowed.');
  
    // Check if we can upload to the specified path, if not DIE and inform the user.
    if(!is_writable($upload_path))
    	die('You cannot upload to the specified directory, please CHMOD it to 777.');

// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES["$img"]['tmp_name']) > $max_filesize)
	die('The file you attempted to upload is too large.');

// Upload the file to your specified path and renames this file to 'profile' and keeps the same extension.
if(move_uploaded_file($_FILES["$img"]['tmp_name'],$upload_path . "$imgname." . $pathinfo['extension']))
	echo 'Upload Successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a><br />';
else
	echo 'There was an error during the file upload.  Please try again.';

?>

 

At the moment it all works fine. I can upload all 3 files to a temporary directory that I created earlier and all are renamed so that I can display them with ease later on. Im after tidying it up a bit now to be honest, ideally on the upload.php page.

 

What Im wanting to do is combine this part;

<?php
// Configuration of profile image
$filename = $_FILES['userfile2']['name']; // Get the name of the file (including file extension).

if ($filename == $_FILES['userfile2']['name']){

	$img = "userfile2";
	$imgname = "userfile2";
	include("includes/test2.php");
}
?>

<?php
// Configuration of profile image
$filename = $_FILES['userfile3']['name']; // Get the name of the file (including file extension).

if ($filename == $_FILES['userfile3']['name']){

	$img = "userfile3";
	$imgname = "userfile3";
	include("includes/test2.php");
}
?>
<?php include("includes/footer.php"); ?>

 

so that there is only one block of code. Is it possible to increment the file names so that theyre simply stored as 1 and 2? If so, how would I go about implementing this? This way it would allow me to extend the form to upload more images without having to add any additional code to upload.php Thanks

Link to comment
Share on other sites

Sure it is possible. Firstly I suggest you change the way you are uploading files since you want to upload multiple files at the same time. check out the code at this page: http://www.phpeasystep.com/phptu/2.html

 

it is a very good tutorial on how to create multiple files upload in php. Once you are done you can use a function (instead of including a file to move the uploaded files to wherever you want).

 

hope this helps

Link to comment
Share on other sites

thanks, i check that link out and gave it a try but to be honest i prefer the original way of doing it as i can upload multiple files at the same time already.

 

all im really wanting to do is be able to shrink the amount of code i use on upload.php and have it automatically rename the files based on how many are uploaded. For example, if 5 files are uploaded then they will be named file1, file2, file3, file4, file5. i think it would be done with an increment feature but not sure what the best way of implementing it would be?  :shrug:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.