PHP
  Home arrow PHP arrow 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
    ( Page 1 of 4 )

    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."

    Code

    For file uploading to work, it must be turned on in your php ini file. And the upload _tmp_dir must be set; in other words the upload directory must exist.

    Below is an extract from a php ini file:

    ;;;;;;;;;;;;;;;;

    ; File Uploads ;

    ;;;;;;;;;;;;;;;;

    ; Whether to allow HTTP file uploads.

    file_uploads = On

    ; Temporary directory for HTTP uploaded files (will use system
    default if not

    ; specified).

    upload_tmp_dir = C:PHPuploadtemp  ; temporary directory for
    HTTP uploaded files (will use system default if not specified)

    ; Maximum allowed size for uploaded files.

    upload_max_filesize = 2M

    *****End php.ini file*****

    Here you can see that all the necessary settings are made.

    Once you've made all the necessary changes to your php settings, let's create the form needed to do the uploads. To enable a form to handle uploads, you need to change three things on a standard form.

    • First, the initial form line must include enctype="multipart/form-data." This tells the browser to expect different types of data.
    • Second, a hidden input type should be included to tell the browser the max file size allowed for uploading. This is not strictly necessary, as users may upload files of varying sizes.
    • Third, the "<input name="userfile" type="file">" is included to create the upload field and also creates the browse button. Below is an example of a upload form.

    Fig1. Upload form example

    PHP fills a $_FILES[''] array with information about the file that you upload. The array contains the following info:

    Name - name of the file as it was on the local disk.

    Type - mime type of the file (example image/gif).

    Size - file size.

    Tmp_name- name of the file as it is stored in the final destination.

    Error - error code for when something goes wrong.

    Code

    Meaning

    0

    No Error

    1

    File exceeds set upload_max_filesize

    2

    File exceeds the max_file_size setting in form

    3

    File was partially uploaded

    4

    No file was uploaded



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





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