Uploaded File Size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Uploaded File Size

Post by AliasBDI »

How can I check for the size of the uploaded file?

php $_FILES["imageFile"]["size"]>30KB

??
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

do a print_r($_FILES) to inspect the format of the array and it's data
The Bat
Forum Newbie
Posts: 14
Joined: Thu Feb 01, 2007 3:57 pm

Post by The Bat »

If you're talking about size validation before the actual uploading, you could use an if statement:

Code: Select all

if ($_FILES['imagefile']['size'] >= 30720) { // 30KB = 30720B
   // Proceed with uploading...
} else {
   echo 'The file is too large. Please try again.';
}
Last edited by The Bat on Tue Feb 13, 2007 2:43 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

30KB is 30720 bytes. ;)

.. and PHP will do the size check (if possible) for you using two directives (upload_max_filesize and post_max_size) and possibly one form submission (MAX_FILE_SIZE). Details can be found here: http://php.net/features.file-upload
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

personally for file size calculation i use:

Code: Select all

$file_size_mb = round($file_size_bytes / 1048576, 2);

echo "$file_size_mb MB";
this outputs the file size in mb with 2 digits after the comma like so
input:
12624855,04 Bytes

output
12,04 MB
Post Reply