Skip to content

Testing PHP MAX_FILE_SIZE

I just thought I’d share a quick way to test your handling of uploaded files that exceed the maximum file size limit.

You’ve written your code to detect if the file size exceeds the limit with something like: $fileTooLarge = array(UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE); if(in_array($_FILES['userfile']['error'], $fileTooLarge, true)) { // Display descriptive message to user about file size exceeding limit. } Let’s say you’re using the default upload_max_filesize of 2 MB. Do you go search your system for a file larger than 2MB that contains no confidential information? I guess you could, but if you’re on a Linux or Mac machine you can use theddtool.

To quickly create a file of 2.5 MB, you would simply type this at the command line: $ dd bs=512k count=5 if=/dev/zero of=testUpload This creates a binary file named testUpload containing zeros using 5 blocks of 512 bytes for a total of 2.5 MB.

Now we all know that there’s no way we’re going to remember this unless you use it constantly. My solution to this is by creating a plain text file named thingsIforget.txt in my References folder. Having this file has been a huge time saver over and over again.

Well, I hope a couple of you find this useful.

Facebooktwitterredditlinkedin

Published inWeb Development

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *