Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101993 Viewers
61 Video lessons · 88702 Viewers
71 Video lessons · 72528 Viewers
56 Video lessons · 104187 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.