Viewers: in countries Watching now:
The basic process of uploading files with PHP is very simple, but there are security implications that many people are unaware of. This course shows how to create a secure custom PHP class that can handle both single-file and multi-file uploads. Author David Powers shows you how to create a file upload class that checks the size, type, and names of files, renaming them when it encounters a duplicate file name. He'll show you how to make the class report on the outcome of the upload process and the nature of any errors that occur, and how to prevent the user from uploading files that exceed the server limits.
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.
In the previous chapter, we created a basic script to upload the single file. But it was far from perfect. Although there was a hidden form field to set the maximum size for an individual file, someone could easily create a form of their own to get around that restriction. No checks are made on the type of file being uploaded. If the files are stored in the public folder, this presents a security risk. There's no check on filenames. So they could contain spaces, causing problems on Linux servers or in URLs.
Also, files with the same name as an existing one, automatically overwrite the original. The basic script handles only one file at a time. And it would be difficult to reuse without adapting the same script each time you want to use it. To solve these shortcomings, we're going to create a custom class. A class is basically a collection of related variables and functions that are designed to work together to perform a specific task, in this case to upload files.
Variables defined inside the class are known as properties. Functions inside a class are known as methods, but they're defined in exactly the same way as any other function. Calling the method as simply a convention. In php, you can set the visibility of properties and methods to public, protected or private. Public means they can be freely accessed. Protected or private hides them from strips outside the class definition, preventing values from being changed accidentally.
You use the class to create an object. You store this in a variable like any other value, but it gives you a strip access to the classes properties and methods. We'll be creating a class called UploadFile. The first requirement is that it should be easy to reuse. It needs to check the maximum size on the server side to avoid running out of disk space. The class should restrict uploads to specific mime types, and if that's too restrictive, it should neutralize potentially risky types.
For example, if an executable file is uploaded, adding a suffix to the file name will prevent it from being run automatically. Filenames need to be cleaned up by removing spaces. There also needs to be an option to check for duplicate file names ,and add a number to the file's base name to prevent existing files from being overwritten. The class also needs to handle multiple uploads seamlessly. To avoid conflicts with other scripts the class will use a namespace. This means the server must be running php 5.3 or later.
Official support for earlier versions of php ended in December 2010. So if your server is still running PHP 5.2 you're well overdo an upgrade. Like the original script, the class needs to inform the user of the outcome of the upload. And the class will need some helper methods such as converter's too and from megabytes. Which can also be used without creating an object. There's a lot of work to be done. So let's get on with building the class.
There are currently no FAQs about Uploading Files Securely with PHP.
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.