Simon336697
asked on
Turning this upload script from "1 file upload capable" to "multi-file upload capable"
Hi everyone hope you are all well.
Guys I have the following php script, that currently is only 1-file-upload capable.
Id like to modify it so that it is mulit-file upload capable....let's say 5 at a time.
I have included the script in the code snippet.
Any help greatly appreciated.
Guys I have the following php script, that currently is only 1-file-upload capable.
Id like to modify it so that it is mulit-file upload capable....let's say 5 at a time.
I have included the script in the code snippet.
Any help greatly appreciated.
=========================================== upload.php:
<!-- ******************************************************
HTML FORM SECTION:
*********************************************************** -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<p>
<label for="file">Select a file:</label>
<input type="file" name="userfile" id="file"><br />
<input type="submit" value="Submit" />
<p>
</form>
<!-- ******************************************************
UPLOAD SECTION:
*********************************************************** -->
<?php
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 524288;
$upload_path = './files/';
$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>';
else
echo 'There was an error during the file upload. Please try again.';
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes, you could put the current code into a foreach loop and step through each $_POST variable to get the names of the form fields, but you would then need to call your file input boxes something unique/easy to check for, since $_POST will contain all form elements.
So, you could do something like this maybe:
So, you could do something like this maybe:
foreach($_POST as $key=>$val) {
if (($val == "userfile1")||($val=="userfile2")||($val=="userfile3")||($val=="userfile4")||($val=="userfile5")){
$filename = $_FILES[$val]['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
if(filesize($_FILES[$val]['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
if(move_uploaded_file($_FILES[$val]['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>';
else
echo 'There was an error during the file upload. Please try again.';
}
}
ASKER
psimation, thats great...thanks so much for your skillful answers...really appreciate it psimation.
ASKER
Thanks also to:
jericotolentino and babuno5
jericotolentino and babuno5
No Problem Simon
Just remember to close the question by accepting one or more of the comments as solutions - else the question will remain open on the system.
Just remember to close the question by accepting one or more of the comments as solutions - else the question will remain open on the system.
ASKER
Thank you guys :>)
ASKER
Thanks for that.
Is there any way instead of duplicating the code, to loop through files for multiple input boxes?