In this lesson our instructor talks about HTTP POST: File Uploads. First, he discusses POST Data Encoding, uploading files, $_FILES superglobal, and structure of $_FILES. Then he talks about moving an uploaded file. He ends the lesson with a homework challenge.
When using the POST method to submit data from an HTML form, the data is encoded using
URL-encoding by default.
When uploading files using HTTP POST however, the form’s data must be encoded differently.
The form data should be of the content type:
This is done by setting the enctype attribute of the HTML <form> tag to
An HTML <input> tag with its type attribute set to file is used to
upload files from an HTML form.
In PHP, information about files uploaded to a script are accessed using the $_FILES superglobal.
The superglobal is a multi-dimensional associative array where the keys are the names of any file input tags
submitted on a form and the values are associative arrays containing information about each file uploaded.
The associative array provided for each file uploaded by $_FILES has the following keys:
name – original filename of the uploaded file
type – content type of the uploaded file
tmp_name – an absolute path to the location where the file has been temporarily uploaded
error – error code for file upload
size – number of bytes of the file uploaded
An error code of 0 means a file was successfully uploaded.
Uploaded files are stored in the temporary directory of the environment PHP is running in. This directory can
be changed by modifying the upload_tmp_dir directive in ‘php.ini’. In XAMPP, this
directive has been set to: 'xampp/tmp'.
move_uploaded_file() will move an uploaded file from the temporary upload directory to the
directory of your choice. Note: the user account PHP is being run as must have write permissions on the
directory the file is being moved to.
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.