Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So let's take a minute to talk about how file uploads work between forms and PHP. In addition to capturing regular text data, a form can also send a file into a folder on your server using PHP. In order for this to work, the form has to use the multipart/form-data enctype. This is part of your form tag. Because get methods are limited to the size that's allowed when sending data through a URL, you should use the post method if you're uploading files from a form. You can include the accept attribute inside the input field to limit the MIME type of the file you're uploading. If you have any questions about this make sure you check out the movie on Limiting Uploads by MIME Types.
When uploading files, you can use an optional hidden field with the name of Max File Size that PHP commands will try to recognize. So for example this input field tells PHP that this file should be no bigger than about 30K. One note, this hidden input field has to be placed before the file input field. Once a form is submitted, PHP stores information about the uploaded file in a special files associative array. You can use this superglobal in your script to get information about the file.
You can read more about this on this page in the PHP documentation. Here's some different variables that you can get and what they do. So let's go over those. You can read the original name of the array using the user file name field. If the browser provides it, you can also read the MIME type of the file by reading the user file type value. The user file size value holds the size in bytes of the uploaded documents. PHP is going to place the uploaded documents in a temporary place on the server.
This variable, user file temp name, has the name of that location. What you'll end up doing, is moving the file to a more permanent location later. If the server is unable to upload the file for any reason, you can check the error types on this page. After you check, validate and sanitize file info using the file's superglobal. The file has to be moved, and you can do so with the move_uploaded_file function. You can read more about this function in this page of the documentation. It's really pretty simple.
The function makes sure that, if the file was uploaded properly, it moves the file to a permanent position on your server. This position must have the right upload permissions, which you have to set manually. If the file upload and movement were successful, the function returns true. So you usually check this function with an if statement. It's really important to give your filenames a unique name, because this function will simply overwrite any files at the destination with the same name, and it doesn't give you a warning. So one of the reasons I love PHP is its ability to make complicated tasks really simple.
Working with files is a good example. It takes care of most of the heavy lifting while providing easy functions to get work done. In the next movie, we'll add this functionality to our form.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101328 Viewers
61 Video lessons · 88090 Viewers
71 Video lessons · 71944 Viewers
56 Video lessons · 103758 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.