Dynamic image upload
Posted: Sun Mar 28, 2010 2:55 pm
Right, ive been playing around with uploading files to newly created folders...given the users id and name etc.
At the moment they can upload 5 images on the form:
Then on the upload.php page I have a few different varieties of validation etc...which to be honest is quite a few lines of code. eg;
Now as you can see, the above code is just for the first image that is uploaded. There are an additional 4 chunks of code for the others, and the only thing that is different between them is the part.
How would I go about creating a dynamic check for each image, but with only using 1 block of code? Is this possible?
thanks
At the moment they can upload 5 images on the form:
Code: Select all
<form action="./upload.php" method="post" enctype="multipart/form-data">
<p>
<label for="file">Select file 1:</label> <input type="file" name="userfile1" 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 />
<label for="file">Select file 4:</label> <input type="file" name="userfile4" id="file"> <br />
<label for="file">Select file 5:</label> <input type="file" name="userfile5" id="file"> <br />
<button>Upload File</button>
<p>
</form>
Code: Select all
<?php
// Configuration - Your Options
$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
$filename = $_FILES['userfile1']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// 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['userfile1']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile1']['tmp_name'],$upload_path . $filename))
echo 'Upload was 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.';
?>Code: Select all
$_FILES['userfile1']How would I go about creating a dynamic check for each image, but with only using 1 block of code? Is this possible?
thanks