Slideshow Image Slideshow Image Slideshow Image Slideshow Image Slideshow Image
Creating a Multi-File Upload Script in PHP
Date Added: 01-01-1970
Hits: 1197
Submited by:

Meena Sharma

  Print this    Email this

Frustrated with single-file upload scripts? Looking for an alternate route? Read as Jonathan shows us how easy it really is to setup a multi-file upload script using PHP.
As a PHP programmer I had run into a problem where a client needed a form to upload more than one file at a time. So one night I sat down and spent an hour figuring out the best and easiest way to do this. In this tutorial, the for loop is going to be your best friend.

uploadForm1.php


# of Files to Upload





Enter the amount of boxes you will need below. Max = 9.












As you can see this first page is very basic. In my form I set the uploadNeed maxlength to 1. This way the max upload boxes he or she can get is 9. You can increase or decrease this to satisfy your own project needs.

uploadForm2.php
Ok, this page will be doing one half of the work. We will be using the for loop to get this task done.



Untitled Document







// start of dynamic form
$uploadNeed = $_POST['uploadNeed'];
for($x=0;$x<$uploadNeed;$x++){
?>


// end of for loop
}
?>








In this page, all I did was create a simple HTML form with the value of the attribute "type" set to "file". Within the form I put a block of code to start the for loop. I set $x to 0 and I made it stop at the desired need by setting $x to be less than $uploadNeed – the value specified by the user. I 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 I am 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.

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.

This little script I am sure will come in handy when you have multiple files that you want to upload all at once. Some other things you could add to this script are listed below.

Insert file names into a mysql database.
Get the size of the file and store that in the database too.
Create a temp text file delimited by commas for the files that have trouble uploading and at the end of the script do a retry on those files.
Create a mass-delete of files.
.

 

Login Now!
Email:
Password:
Forgot Password New User? Sign up

Featured Category
Art.. Multimedia..
Photography.. Jobs..
Real Estate.. Investment..
Software.. Search Engine Optimization..
PPC Consultancy.. Web Solutions..
Video Games.. Poker..
Fitness.. Medicine..
Alternative.. Family..
Matrimonial.. Cooking..
Education.. Teen..
Teen Life.. Media..
Newspaper.. Weather..
Funny.. Entertainment..
Movie.. Travel..
Hotel.. Tour Operators..
Tour Guide.. Transport..
Regional.. Science..
Jewelry.. Gifts..
Ecommerce.. Society..
Sports.. Small business..
Professional Services.. Stock Exchange..
Manufacturing.. Exporters..

Featured GOLD Listing
knjassociates.com

Income Tax Services Delhi, Income Tax Consultant India, non resident income tax

professionalclick.com

professional click is a site to store all the personal information of profession

ratnatourandtravels.com

Travel agents in delhi, car rental delhi, Ratna Tour and travels is a leading to

linkinfotech.com/

Link Information Technology Private Limited - Offering outsourcing business mark

kdrservices.co.in/

Complete office job outsourcing includes accounting, Finance, arts, Entertainmen

thetravelboss.com/

Travel directory of online travel site and get travels links along with other t

arb-bearings.com

Much of ARB's success as a bearing manufacturer is largely due to its passionat

hansindia.com/

Hans is a Professional Website Design Company Affordable Custom BEST website des

thisisbs.ca

ThisisBS was created for the customer by a customer. It was made for the custome

vikrampalmist.com/

Astrology Consultation in Delhi India, Astrology in Delhi, Delhi Astrology Consu

rajdhanienterprises.com

There is a difference in industrial hard chrome plating. Since our beginnings in

webinfology.com

Customization, Drupal, DNN, X-cart, Zen cart, Site, PHP, PHPBB, PHP Nuke, Compan

biharlokmanch.org

Visit the site to know Bihari Durga Puja, how we celebrates, our history, cultur

tstbq.com

The Spanish Travel Boutique is a travel boutique which specialises on routes and

Latest Submission
http://focusacne.com  Health
http://www.purnimaexports.com  Business
http://www.thedesignerdiscount  Shopping
http://www.prayaanika.com/  Regional
http://www.jenlyfavors.com/  Shopping
Advertisement