/    Sign up×
Community /Pin to ProfileBookmark

Check File Size before Upload

Hi everyone,

I have facing this problem for a few days already. In my currently project, I really want to check the file size (is it too big) before it send or completely send to the server.

I know PHP cannot do that because it is server side program language. can anyone give me solution ? any free 3rd party file upload component for php5 can solve this problem?

Cheers,
Bryan

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@The_Little_GuyJan 23.2007 — Something like this may help

[code=php]
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
$filesize = filesize($tmp_name);
if($filesize > 1048576){ #I believe this is 1MB (Measured in bytes)
move_uploaded_file($tmp_name, "data/$name");
}else{
echo'File Too Big<br>';
}
}
}
[/code]
Copy linkTweet thisAlerts:
@bryanevilauthorJan 23.2007 — Something like this may help

[code=php]
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
$filesize = filesize($tmp_name);
if($filesize > 1048576){ #I believe this is 1MB (Measured in bytes)
move_uploaded_file($tmp_name, "data/$name");
}else{
echo'File Too Big<br>';
}
}
}
[/code]
[/QUOTE]


Thanks, but this only work when the file is completely sent to server. I want the check happen before the file is send.
Copy linkTweet thisAlerts:
@The_Little_GuyJan 23.2007 — That then you probably need to use javascript
Copy linkTweet thisAlerts:
@bryanevilauthorJan 23.2007 — But javascript cannot read the local computer file system
Copy linkTweet thisAlerts:
@The_Little_GuyJan 23.2007 — Then I don't think that it is possible, maybe with Java though
Copy linkTweet thisAlerts:
@NogDogJan 23.2007 — Make sure your form has the following hidden input element [i]before[/i] the file input element:
[code=html]
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
[/code]

Change the value to whatever max file size (in bytes) you want to enforce.
Copy linkTweet thisAlerts:
@NightShift58Jan 23.2007 — Unfortunately, only an Applet or ActiveX...
Copy linkTweet thisAlerts:
@pcthugJan 23.2007 — Use NogDog's suggestion; place the following hidden input element within your form:
[code=html]<input type="hidden" name="MAX_FILE_SIZE" value="30000">[/code]Keep in mind that the MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted. This is an advisory to the browser, PHP also checks it. Fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. The PHP settings for maximum-size, however, cannot be fooled. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too big and the transfer failed.
Copy linkTweet thisAlerts:
@bryanevilauthorJan 23.2007 — thank you everyone, but it is a big trouble to let javascipt access local user driver. I know there are script that can read the file header first while the file is sending to server (not read the file from user driver), I also know Perl can do that too, but i know nothing about Perl.

and the name="MAX_FILE_SIZE" value="30000" is not safe as well if someone is trying to crash your server i think.

Please give me more alterative please. Thank You
Copy linkTweet thisAlerts:
@bryanevilauthorJan 23.2007 — Furthermore, I heard about Ajax can do something similar , is that true? how gmail or window live check the file size? they use ajax too?
Copy linkTweet thisAlerts:
@NogDogJan 23.2007 — AJAX is just the use of certain JavaScript methods to send/receive messages from the browser to the server. It will therefore be as susceptible to misuse as any other client-side solution (or complete non-functionality if the user has JavaScript disabled).

All I've ever done is to set the MAX_FILE_SIZE hidden field in the form and then also set the same limit for the [url=http://us2.php.net/manual/en/ini.core.php#ini.upload-max-filesize]upload_max_filesize[/url] PHP value in a .htaccess file (could also be done in php.ini if you have access to it) to make sure the server won't accept anything larger.
×

Success!

Help @bryanevil spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...