Join David Powers for an in-depth discussion in this video How PHP handles file uploads, part of Uploading Files Securely with PHP.
- View Offline
To upload files with PHP, you need to create a web page with a form and file input field. In my editing program, I've created a page called form.php, and I've saved it in a folder called Uploads, inside the Server Root. So let's create the form. I'm going to maximize my Editing pane, so we can see the code more clearly. And the form will go here, underneath the h1 heading. Form, and the action attribute tells the browser which processing strip to use when the form is submitted. We're going to make this a self processing form.
So add a PHP block inside the quotes, then echo and the server super global array element that we want is PHP_self, all in caps and in quotes. And what this will do, is it will reload the same page when the form is submitted. And we'll add the processing script in a PHP block above the doc type. Also in the opening form tag, we need to add the method attribute.
This can be, get or post. When you're uploading files, it must be set to post. And then there's one other thing that we need in the Opening Form tag. It's the enctype attribute. I mean most forms, you don't need the enctype attribute, but you do need it when you're uploading files. So let's just add that enctype. My editing program has added in the default value. For files you need to change that to multipart/form-data.
So that's the form tag dealt with, let's just put our fields inside the form. And we'll put them in a paragraph, so that they can be nicely formatted. And we'll have a label, and we'll use the For attribute for the label. The For attribute take the ID of the target element. So we're going to call it, file-name, and then inside there, we'll put Select File. And we need our Input element. And the type needs to be File.
And it needs a Name attribute. We'll use the same as the ID, which will be file-name. And then the ID was a label. Then in another paragraph, let's have the Submit button. Type will be Submit, and the name will be Upload. And then we'll have a value, which will be what will be shown on the button. Which will be Upload File. And then we just needed a Closing Paragraph tag there.
So that's our form created. Now in this form all I've got is a File Input field and a Submit button. But you can include other Input fields in the same form as the File field. You don't need to create a second form for file uploads. So let's just save this, and then we'll test it in the browser. So here it is in my browser. Now there's actually no field here. There's just a button called Choose File. I'm using Chrome. Other browsers might show you Browse, instead of, Choose File.
And Internet Explorer does still display a File Input field, but it also automatically adds a Browse button. So let's just choose a file. I'll go to my desktop, and then to the folder where I've got a lot of files ready. And just select a file, and then click Upload File. And the file name disappears, it appears as though nothing has happened. In one sense, that's true, because we don't have a file processing script, but that doesn't mean that the browser simply ignored the file.
When you click the Submit button, the Browser sends a copy of the file with the post data, and PHP sends it to a temporary folder ready for processing. The location of Temporary folder, depends on your Server configuration. You don't normally need to know where it is, but you can find out where it is, by running PHP info. So let's just go back to my Editing program. I got this page here config.php with PHP info in it. Let's just run that. Here, we've got the configuration for my server, and if we scroll down to the Call section, and then just increase the size a little bit, so we can see better.
And there it is, upload_tmp_dir. On my system, it's in the C drive in xampp and temp. So let's just open Windows Explorer and see what's actually happened there. So I need to go to my C drive and then down at the bottom xampp, and then the temp folder, and it's not there. I've got some other files in the Temporary folder, but there's no sign of that image. And the reason for that is that the PHP discards uploaded files immediately if you haven't created a processing script.
So that's our next task. But before we start work on the script, let's just recap some important points about file uploads with PHP. The upload form could always use the POST method. You need to add the enctype attributes of the opening form tag and set its value to multipart/form-data. This tells the browser how to handle any files submitted with the form. There's no need to create a Choose File or Browse button.
Browsers automatically do this when you set the Type attribute of an input element to file. When the form is submitted, the file is uploaded with the post dated to a Temporary folder, but is discarded if you don't process it immediately.
At the end of this course, you'll have a robust, flexible class that can be incorporated into many projects (including web forms) with just a few lines of code.
- How PHP handles file uploads
- Setting the maximum file size
- Moving the file to its destination
- Creating and using a namespaced class
- Displaying error messages
- Restricting unacceptable MIME types and file extensions
- Using the class
- Reporting errors
- Altering the user