Home arrow PHP arrow Uploading Files and Navigating Directories in PHP

Uploading Files and Navigating Directories in 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

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
 

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: