PHP
  Home arrow PHP arrow Page 2 - Uploading Files and Navigating Directories in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Uploading Files and Navigating Directories in PHP
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-08-30


    Table of Contents:
  • Uploading Files and Navigating Directories in PHP
  • Uploading files
  • Multiple file uploads
  • Navigating Directories

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Uploading Files and Navigating Directories in PHP - Uploading files
    ( Page 2 of 4 )

    When a file is uploaded, it is first placed in a temporary directory. You have to use the move_uploaded_file() function to move it to its final destination. The move_uploaded_file function takes three parameters:

    move_uploaded_file($_FILES['userfile']['tmp_name'],
    '/upload/to/path/'. $filename);

    To demonstrate, create a normal HTML  form, and make the changes we've discussed to turn it into a upload form. Below is a example:

    <html>
    <head>
      <title>Administration - upload new files</title>
    </head>
    <body>
    <p>Upload File</p>
    <form enctype="multipart/form-data" action="upload.php"
    method="post">
      <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
      Upload this file: <input name="userfile" type="file"><br>
      <input type="submit" value="Upload File">
    </form>
    </body>
    </html>

    Now let's create a script to handle the uploaded file:

    Script: do_upload.php
    <?
    //define the upload path
    $uploadpath = "c:uploads";
    //check if a file is uploaded
     if($_FILES['userfile']['name']) {
      $filename = trim(addslashes($_FILES['userfile']['name']));
    //move the file to final destination
      if(move_uploaded_file($_FILES['userfile']['tmp_name'],
    $uploadpath."/". $filename)) {
      echo "The file has been uploaded";
      } else{
        if ($_FILES['userfile']['error'] > 0)
      {
           switch ($_FILES['userfile']['error'])
        {
          case 1:  echo 'File exceeded upload_max_filesize';  break;
          case 2:  echo 'File exceeded max_file_size';  break;
          case 3:  echo 'File only partially uploaded';  break;
          case 4:  echo 'No file uploaded';  break;
        }
        exit;
      }
      }
    }
    ?>

    The do_upload script first sets the upload path by declaring

    '$uploadpath = "c:uploads";

    This is where our files are going to be stored on our C drive. Next we check to see whether the Files array is filled:

    ' if($_FILES['userfile']['name']) {'

    If it is filled, the 'move_upload_file()' function is used to upload the file and a message is shown. In case the array is not filled we define a switch statement with the various error numbers and what they mean:

          case 1:  echo 'File exceeded upload_max_filesize';  break;
          case 2:  echo 'File exceeded max_file_size';  break;
          case 3:  echo 'File only partially uploaded';  break;
          case 4:  echo 'No file uploaded';  break;

    One of these errors will be executed when an error number is found in the File array.

    Here's a screen shot of both scripts in action:

    Fig2. A file is selected for uploading...

     

    Fig3. File upload message...



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek