Home arrow PHP arrow Page 3 - Uploading Files and Navigating Directories in PHP

Multiple file uploads - PHP

In this article we are going to look at how to upload files and also how to navigate through directories. It is the second part of a tutorial that began last week with "Reading, Writing, and Creating Files in PHP."

TABLE OF CONTENTS:
  1. Uploading Files and Navigating Directories in PHP
  2. Uploading files
  3. Multiple file uploads
  4. Navigating Directories
By: Jacques Noah
Rating: starstarstarstarstar / 8
August 30, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Uploading multiple files simultaneously is not very different from uploading a single file. All you need to do is use a different name for each file that you want to use:

<form enctype="multipart/form-data" action="do_upload.php"
method="post">  Upload files:

<input name="Afile[]" type="file" size="80">
<input name="Afile[]" type="file" size="80">
<input name="Afile[]" type="file" size="80">
  <br>
  <input type="submit" value="Upload File">
</form>

When the above form is submitted, the arrays $_FILES['Afile'], $_FILES['A']['name'], and $_FILES['Afile']['size'] will be initialized and all of them will be numerically indexed. For example say you submit three files with the names uploader.html, fileprocess.php and what.php. The $_FILES['Afile']['name'][0] array will contain uploader.html and the  $_FILES['Afile']['name'][1] array will contain what.php, and so on. All of the file attributes such as size and name will be available for each file that you submitted.

Once you've submitted the form you simply do a "for each" loop and display the form information.

Another way in which you can upload multiple files is by explicitly giving the "name" part of the form input a different name:

<form enctype="multipart/form-data" action="do_upload.php"
method="post">  Upload files:

<input name="Afile1" type="file" size="80">
<input name="Afile2" type="file" size="80">
<input name="Afile3" type="file" size="80">
  <br>
  <input type="submit" value="Upload File">
</form>

And then process them as normal.

Create another HTML document and call it uploaderfrm.html. Add the following code:

Uploader.html

<html>
<head>
<title>HTML Form for Multiple File Uploading</title>
</head>
<body>
<p>Multiple File Upload</p>
<br />
<form action="uploader_do.php" method="post"
enctype="multipart/form-data">
<p>Files:<br>
<input type="file" name="AFile[]" /><br>
<input type="file" name="AFile[]" /><br>
<input type="file" name="AFile[]" /><br>
<input type="submit" value="Upload Files" />
</p>
</form>
</body>
</html>

Now, let's create the script that will handle form data. Create a new PHP Document and save it as uploader_do.php.

Script:uploader_do.php

<?php

$uploaddir="c:uploads";
if(isset($_FILES['AFile']['error'])){ 
   foreach ($_FILES["AFile"]["error"] as $key => $error) { 
      if ($error == UPLOAD_ERR_OK) { 
         $tmp_name = $_FILES["AFile"]["tmp_name"][$key]; 
         $name = $_FILES["AFile"]["name"][$key]; 
         move_uploaded_file($tmp_name, $uploaddir. "/" .$name); 
      } 
   }

?>

As with our previous upload script, first we define where we want to store the uploaded files by declaring '$uploaddir="c:uploads";' From this declaration we can see that our files are going to be stored on our C drive. Next we check whether there are any errors in the submitted File array:

'if(isset($_FILES['AFile']['error'])){ '

If there are any errors, the files will not be uploaded. If on the other hand there are no errors, the files are uploaded by the 'move_upload_file()' function.

There's another way that you can dynamically create input fields on a form to upload files. If you present a user with a form asking how many files he or she wants to upload, once you have the number you can simply create a dynamic form with n number of input tags and continue from there.



 
 
>>> More PHP Articles          >>> More By Jacques Noah
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: