From the course: PHP Techniques: Working with Files and Directories

Handling upload errors - PHP Tutorial

From the course: PHP Techniques: Working with Files and Directories

Start my 1-month free trial

Handling upload errors

- [Instructor] We've seen how to handle file uploads in PHP. Let's consider what to do if there's an upload error PHP will detect upload errors and store it in the files super global array for the upload. It stores an integer value the number zero through eight. PHP also has constants defined that are equal to these values. In this chart, the first column is the constant name, and the second column is the actual error value. These are interchangeable though because they have the same value. In the third column, I've put a simple explanation of the error. In the first row, there's an entry for zero which indicates there was no error at all. That's useful because our PHP can check to see if the error code is zero, or if it's greater than zero. Errors one and two are triggered if the file exceeds the maximum file size, error three is for a partial upload, error four is when there's no uploaded file, error five doesn't exist anymore so we don't need to worry about that. Error six appears if there's not a temporary directory to store the uploaded file. Error seven is when PHP can't write to the temporary directory, usually due to a lack of permissions. Error eight is when a file upload is stopped by an extension, something you usually won't need to worry about. If you get an error, you can simply tell the user that something went wrong and not be specific about it. Or you can convert these error messages into text that's suitable for display to the user, it's your choice. At the top of my upload .PHP file I'm going to paste in a custom PHP function. You can pause the movie if you want to copy it down, or it's included in the exercise files for movies after this one. This function is called upload error and takes one argument, and that is the error. That's going to be the status code that was returned in that file, super global. You can see that it sets upload errors equal to an array which is equal to those values we just saw in that chart. We have the constants that PHP has on this side, and then a plain text message on the right. Down here you'll see that it'll take that error and it'll return the value that corresponds inside that upload errors array. Let's go down into our form processing and let's check for that error. So let's first just set the error equal to, and we know what it's going to be, it's going to be very similar to this. I'm going to copy this and paste it in, but instead of temp name, we're going to ask it for error. So that'll get the value and set it here to an easy to use variable, then let's say if there is an error that is greater than zero, remember zero means there's no problem at all, then we have an error, so we're going to display it. We'll call upload_error, our new function, and we'll pass in that error. Else, there's not a problem in which case we will do this form processing right here. There we go. Now it's a little difficult for us to trigger an error on purpose. I suppose you could remove the temporary directory or change its permissions, or you could upload a file that's super large. But I'm not going to do that. We know we have code in place, that handles whatever error PHP might tell us about here. Let's save it and let's test it one more time to make sure that it still works when there is no error. I'll go back to Firefox, I'll hit the Back button so we get our upload form again, and I will browse and choose the sonnet again, click Open and upload. The file upload succeeded. Notice that it did overwrite. That is an important point. When we upload it overwrites the file that's already at that destination location. But it did work, there were no errors, but if there had been we were prepared to handle them.

Contents