Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use $_FILES["file"]["size"]? [closed]

Tags:

how can I see if the file loaded weighs less than 5MB? I am using

$ _FILES ["file"] ["size"]  

but I do not know how to set 5mb, because the value must be expressed in bytes, from what I understand. So how to do

$ _FILES ["file"] ["size"] <(5mb) 

thanks

like image 383
John Titer Avatar asked Feb 07 '13 18:02

John Titer


People also ask

What is the use of $_ files in PHP?

$_FILES is a super global variable which can be used to upload files. Here we will see an example in which our php script checks if the form to upload the file is being submitted and generates a message if true. Code of files.

How do I read file size?

Locate the file or folder whose size you would like to view. Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

What is $_ files [' file Tmp_name ']?

$_FILES['file']['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.

How can upload file size in PHP?

To increaes file upload size in PHP, you need to modify the upload_max_filesize and post_max_size variable's in your php. ini file. In addition, you can also set the maximum number of files allowed to be uploaded simultaneously, in a single request, using the max_file_uploads .


2 Answers

To keep code clear, I often define units as constants:

define('KB', 1024); define('MB', 1048576); define('GB', 1073741824); define('TB', 1099511627776); 

Then you can simply do your condition like

if ($_FILES['file']['size'] < 5*MB) 
like image 103
Martin Avatar answered Oct 06 '22 16:10

Martin


5MB -> 5 * 1024 * 1024 bytes

or... if you're a storage vendor, then it's actually 5 * 1000 * 1000.

like image 37
Marc B Avatar answered Oct 06 '22 16:10

Marc B