Creating a Multi-File Upload Script in PHP
From this article, we can learn how to upload more than one file at a time.
Step 1 : Basic form to get no.of Upload file required (uploadform1.php)<html>
<head>
<title>No.of Files to Upload</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<html><head><title>No.of Files to Upload</title><meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″></head><body>
<form name=”form1″ method=”post” action=”uploadform2.php”>
<p>Enter the amount of boxes you will need below. Max = 9.</p>
<p>
<input name=”uploadNeed” type=”text” id=”uploadNeed” maxlength=”1″>
</p>
<p>
<input type=”submit” name=”Submit” value=”Submit”>
</p>
</form>
</body>
</html>
In above script, we can get maximum number of files need to be uploaded by user. We can set max upload boxes as 9. We can increase or decrease this to satisfy depending upon our project needs.
Step2 : Creating the Dynamic Form (uploadForm2.php)<html>
<head>
<title>Dynamic form</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<html><head><title>Dynamic form</title><meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″></head><body>
<form name=”form1″ enctype=”multipart/form-data” method=”post” action=”processFiles.php”>
<p>
<?
// start of dynamic form
$uploadNeed = $_POST[’uploadNeed’];
for($x=0;$x<$uploadNeed;$x++){
?>
<input name=”uploadFile<? echo $x;?>” type=”file” id=”uploadFile<? echo $x;?>”>
</p>
<?
// end of for loop
}
?>
<p><input name=”uploadNeed” type=”hidden” value=”<? echo $uploadNeed;?>”>
<input type=”submit” name=”Submit” value=”Submit”>
</p>
</form>
</body>
</html>
In this page, We can create a simple HTML form with the value of the attribute “type” set to “file”. Within the form We cen put a block of code to start the for loop. We set $x to 0 and made it stop at the desired need by setting $x to be less than $uploadNeed – the value specified by the user. We also echo the $uploadNeed into a hidden input field to be carried over to the last page.
The key to making this all work however is the $x variable We are echoing right next to the uploadFile name. What this will do is append a number starting with 0 to the name. This in turn will make each upload field’s name unique.
Step3 : Creating a Multi-File Upload Script in PHP (processFiles.php)Here is the last page to complete our multiple upload tasks.
Here is the last page to complete our multiple upload tasks.<?
$uploadNeed = $_POST[’uploadNeed’];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES[’uploadFile’. $x][’name’];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace(”‘”,”",$file_name);
$copy = copy($_FILES[’uploadFile’. $x][’tmp_name’],$file_name);
// check if successfully copied
if($copy){
echo “$file_name | uploaded sucessfully!<br>”;
}else{
echo “$file_name | could not be uploaded!<br>”;
}
} // end of loop
?>
The first thing we do in this page is grab the uploadNeed from uploadForm2.php. We setup our for loop in the same fashion as the last page. The difference here though is we get the $_FILES name within the for loop. We assign this to the local variable name $file_name.
Next, we do a little parsing by adding the stripslashes and str_replace functions. The reason we add the stripslashes is due to file that may have apostrophes in their name; otherwise this will generate a parse error and prevent that file from being uploaded.
Notice once again how I add the $x variable, which in turn is a number, to the name of the $_FILES. By doing this the script now knows which file it is uploading.
We will use the copy function now to actually begin the upload process. The last thing We added was a simple if statement to check that the copy was successful and We echo that message out to the screen.
