PHP
  Home arrow PHP arrow Uploading Files and Navigating Directo...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
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? 
PHP

Uploading Files and Navigating Directories in PHP
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Nice article. One thing I would like to add is that it's very hard to see the upload...
       · Will look into it. I think PHP comes with certain add ons that have that kind of...
     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT