Creating a Multi-File Upload Script in PHP - Script 3

Started by ganeshbala, Apr 19, 2008, 08:22 PM

Previous topic - Next topic

ganeshbala

Creating a Multi-File Upload Script in PHP - The Big Copy Bang Page

processFiles . php

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!
";
}else{
echo "$file_name | could not be uploaded!
";
}
} // 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. I 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 I added was a simple if statement to check that the copy was successful and I echo that message out to the screen.