We launched a new IT training category! Check out the 140+ courses now.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Changing the class's defaults

From: Uploading Files Securely with PHP

Video: Changing the class's defaults

The public methods of the upload file class have If you plan to add to the end of this array, don't forget and it reports it then you will be able to upload any type of file.

Changing the class's defaults

The public methods of the upload file class have been designed to make it simple to change most defaults. But you might want to change some of them permanently, also the list of permitted Mime types and of potentially risky file name extensions, have been hardcoded into the class definition. To make changes, open uploadfile.php. And most of the defaults can be changed in the property declarations at the top of the class definition. First one that you might want to change is this one here, maxSize.

This sets the maximum limit for individual files that can be uploaded. It's expressed in bytes, and is currently set to 50 kilobytes. It's important to note that the value of max size must be an integer. You can't use calculation. So for example, if you want to change this to 150 kilobytes, and you try to do this, 150 times 1024, you'll see that I get a little underline here, and a message here saying that I've got a syntax error.

You can't have a calculation, it must be a fixed value. So if you want it to be 150 kilobytes, you would need to put in the correct value, which is 153,600. Another important thing to note about maxSize is, it can't be greater than the value of uploadMaxFileSize in the server configuration. Refer to the movie about limitations on file uploads in Chapter 1 if you need reminding of how to check the server limit. The next default that you might want to change is the list of Mime types in the permittedTypes array.

If you plan to add to the end of this array, don't forget to put a comma at the end of line 14 before adding extra types. You can find a full list of mime types on the website of Iana, the internet assigned numbers authority. If we just scroll down here, you can see that the list is absolutely huge. So it's difficult to select the right Mime types to cover all common file formats. Another consideration is that browsers don't always report the official Mime type.

You can use mime_test.php, which you can find in the exercise files for this video to check what browsers report. Let's just try this. I'm using Chrome at the moment. Let's select a zip file and it reports it as being application/octetstream, not application/zip, which is the official MIME type for a zip file. But let's see what Internet Explorer 11 does. Just select that same .zip file and test it.

And it's reported as being application/x.zip-compressed, so very different there. Now let's see what happens if we select a webp file, that's an image type file. Check that, and it's application of eb stream. But in Chrome, that webp file is reported as image/webp. So there are lots of problems with relying on what the browser says the Mime type is. Because of the problems identifying files by their Mime type, you might decide to turn off type checking in the UploadFile class.

You can do that quite simply by changing type checking on to false, so that's on line 17 here. If you change this to false, then Mime types will not be checked and it doesn't matter what you have in this permitted types array. So if you do change this to false, then there is no need to use the allow all types method, unless you want to change the suffix added to potentially risky file name extensions or to upload files without adding a suffix.

The list of potentially risky file name extensions is in the not trusted property. That's this array here on line 18. Just add or remove extensions to suit your needs. But note that the array consists of extension names without a leading dot. But the default suffix which is on the following line, must have a leading dot. So the default suffix at the moment is .upload. And if you don't want to add a suffix to any files ever, you can simply turn notTrusted into an empty array.

So if we just delete everything inside here, then you will be able to upload any type of file. There'll be no type checking whatsoever. One other default that you might want to change is whether the class renames duplicate files, and that's controlled by the upload method. So we need to go down to the upload method, which is down here on line 84. The default value of renameDuplicates is set to true. If you don't want files to be renamed, just set that to false and the class will automatically overwrite existing files.

So that's a quick overview of the defaults that you might want to change in the upload file class. Deciding the default behaviour of a class is a subjective issue, so feel free to adopt the upload file class to suit your own needs. The internal properties method have all been declared as protected, so you could also create a sub-class to add extra functionality or change some of the defaults.

Show transcript

This video is part of

Image for Uploading Files Securely with PHP
Uploading Files Securely with PHP

33 video lessons · 2379 viewers

David Powers
Author

 
Expand all | Collapse all
  1. 4m 49s
    1. Welcome
      57s
    2. What you should know before watching this course
      2m 0s
    3. Using the exercise files
      1m 52s
  2. 33m 2s
    1. How PHP handles file uploads
      6m 16s
    2. Examining the $_FILES array
      5m 8s
    3. Setting the maximum file size
      5m 36s
    4. Preparing the upload folder
      3m 18s
    5. Moving the file to its destination
      6m 51s
    6. Limitations on file uploads
      5m 53s
  3. 47m 3s
    1. Planning the class's features
      3m 15s
    2. Creating and using a namespaced class
      5m 25s
    3. Creating the class constructor
      7m 26s
    4. Getting a reference to the uploaded file
      5m 9s
    5. Checking the error level
      5m 7s
    6. Displaying errors and other messages
      4m 51s
    7. Setting and checking the maximum file size
      7m 19s
    8. Strengthening the setMaxSize() method
      8m 31s
  4. 35m 30s
    1. Restricting acceptable MIME types
      5m 27s
    2. Removing spaces from file names
      5m 21s
    3. Restricting acceptable file-name extensions
      6m 10s
    4. Neutralizing potentially dangerous uploads
      5m 54s
    5. Renaming files with duplicate names
      7m 58s
    6. Moving the file to its destination
      4m 40s
  5. 10m 25s
    1. Understanding how the $_FILES array handles multiple files
      4m 42s
    2. Adapting the class to handle both single and multiple uploads
      5m 43s
  6. 38m 15s
    1. Overview of the UploadFile class
      5m 9s
    2. Setting up to use the class
      4m 11s
    3. Using the class
      8m 12s
    4. Reporting errors with multiple uploads
      4m 0s
    5. Displaying the server limits
      4m 51s
    6. Alerting the user about exceeding the server limits
      6m 14s
    7. Changing the class's defaults
      5m 38s
  7. 1m 38s
    1. Goodbye
      1m 38s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Uploading Files Securely with PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.