Tell A Friend
Message
Your Name
Email
Friend's Email
Your code
Enter the code above
 
Contact Form
Message
Your Name
Email
Your code
Enter the code above
 
New Links
Enter your email to Receive Free E-mail Updates (New Links)
Add to Google Reader or Homepage Add to My AOL Subscribe in Bloglines
ADD Your Link For FREE

Lunarpages.com Web Hosting Create your own FREE Website
Home >> Previous Page >> Article:

Creating a Multi-File Upload Script in PHP New

  Links
 ASP.NET Web Hosting
 Budget Web Hosting
 Business Web Hosting
 Dedicated Server
 Domain Registration
 Ecommerce Hosting
 Email Hosting
 Free Hosting
 Managed Hosting
 Online Backup Storage
 Personal Web Hosting
 Reseller Hosting
 Shared Web Hosting
 Small Business Hosting
 UNIX & LINUX Web Hosting
 Virtual Private Server VPS Hosting
 Web Development Resources
 Windows Web Hosting
  News
 PHP
 Web Development General
  Articles
 Ajax
 JavaScript
 PHP
 Web Development General
RSS Feeds - Links
New links
Editor's pick
Popular links
RSS Feeds - Videos
New videos
Editor's pick
Popular videos
RSS Feeds - News
New items
Editor's pick
Popular items
RSS Feeds - Articles
New articles
Editor's pick
Popular articles

Creating a Multi-File Upload Script in PHP New
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.

Creating a Multi-File Upload Script in PHP

 New


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.

Creating a Multi-File Upload Script in PHP - Script 1: Number of Upload Boxes Required

 

uploadForm1.php

<html>
<head>
<title># 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>

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.

Creating a Multi-File Upload Script in PHP - Script 2: Creating the Dynamic Form

 

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.

<html>
<head>
<title>Untitled Document</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, 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.

Creating a Multi-File Upload Script in PHP - Script 3: 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!<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. 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.

Creating a Multi-File Upload Script in PHP - Conclusion

 

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.
 


 


 


 



Tags:   PHP
 PDF  Print this article
Bookmark and share this article:   Furl  Delicious  Reddit  Facebook  Technorati  BlinkList  Digg  Google  StumbleUpon  Yahoo 
  Top Tags
How jQuery Zone: News: the WordPress Symfony Community Releases Site and Developer with Brian from Web Blog: PHP Week for Latest Framework Your using Zend
  Top Rated Links
 Open Source Ajax Javacripts Projects
 ClearBudget
 Vaan Web Design
 Spydermate | Seo Analysis Tools
 Web Development And Design - Unmotivated Genius
 JavaScript Html Object Referenceâ„¢ (JSHOR)
 My Library
 Free PHP Contact Form Script
 dirLIST - PHP Directory Lister
 Website Design Perth
  Popular Links
 Free Web Hosting - Free PHP Hosting, MYSQL - Zymic
 AnalogX
 Ash.MVC
 ASP Translator
 BuildExec
 DBG
 DzSoft: PHP Editor
 eAccelerator
 EasyPHP
 EngInSite: PHP Editor
  Top Rated Videos
 New in Maya 2009: Preserve UVs.
 Save your style, Brush, Gradient, Shape in the Adobe Photoshop
 Photoshop CS3 Animated Signature Tutorial
 macromedia flash animation tutorial
 Lets make a better sprite comic! (2/3)
 This is a tutorial on how to download Flash 8 pro Dreamweaver 8 pro and fireworks.
 Noah's Tip: How To Make An Adobe Style Icon In Adobe Photoshop 7 And Up
 [HD] Spry Drop Down Menus & CSS Dreamweaver Tutorial
 Optimizing images for the web using Dreamweaver And Fireworks
 The Bulb Web Tutorial Squad Intro.
  Popular Videos
 [How To] Photoshop CS4 Transparent Background.
 Photoshop CS3 Animated Signature Tutorial
 This is a tutorial on how to download Flash 8 pro Dreamweaver 8 pro and fireworks.
 Optimizing images for the web using Dreamweaver And Fireworks
 macromedia flash animation tutorial
 New in Maya 2009: Preserve UVs.
 The Bulb Web Tutorial Squad Intro.
 photoshop animation tutorial
 Save your style, Brush, Gradient, Shape in the Adobe Photoshop
 Noah's Tip: How To Make An Adobe Style Icon In Adobe Photoshop 7 And Up
  Top Rated News
 php|architect: Ext4Yii, bridging PHP and JavaScript frameworks together
 Basic Tests for Forum Implementation
 Forum Implementation
 Adding an RSS Feed to an Online Book Catalog
 Browsing and Searching an Online Book Catalog
 Building an Online Book Catalog
 Gennady Feldman's Blog: Leveraging Oracle connection metadata functionality
 Keith Casey's Blog: Event Driven Programming
 Brandon Savage's Blog: Revisiting: Why Every Developer Should Write Their Own Framework
 PHPBuilder.com: Use PHPUnit to Implement Unit Testing in Your PHP Development
  Popular News
 Adding an RSS Feed to an Online Book Catalog
 Keith Casey's Blog: Event Driven Programming
 Brandon Savage's Blog: Revisiting: Why Every Developer Should Write Their Own Framework
 Forum Implementation: Viewing Posts
 Gennady Feldman's Blog: Leveraging Oracle connection metadata functionality
 WordPress Blog: PHP 4 and MySQL 4 End of Life Announcement
 PHPBuilder.com: Use PHPUnit to Implement Unit Testing in Your PHP Development
 Building an Online Book Catalog
 NETTUTS.com: Why you Should be using PHP's PDO for Database Access
 Forum Implementation
  Top Rated Articles
 Tips for Speeding Up your PHP Code
 Creating a Multi-File Upload Script in PHP New
 PHP for Beginners by a Beginner: Simple Login, Logout, and Session Handling New
 Regular expressions in JavaScript
  Popular Articles
 PHP for Beginners by a Beginner: Simple Login, Logout, and Session Handling New
 Creating a Multi-File Upload Script in PHP New
 Regular expressions in JavaScript
 Tips for Speeding Up your PHP Code
  In The News
Loading...

 

www.fordevr.com

TOS | Refund Policy