Saturday, August 20, 2005

File Upload Handle In PHP: Some tricky functions

I try to say few words here. I Just want to show some problem and Ideas on File Upload Hanlde in PHP and some samples – asap (as simple as possible):
Upload Limit Size There are 3 kind of limits:



  • Limit Specified in HTML code
  • Limit specified by you-PHP program
  • Limit specified of upload_max_filesize in PHP Configuration Directives.
The Limit by HTML form is useful to fast check upload file. But this can be trick if someone modifies the value in HTML code. So it's better recheck the value in PHP.
But in some cases, the value you specified exceeds the value upload_max_filesize.
The question is how to check this value?
You can use the following functions:


function return_bytes($val)
{
$val = trim($val);
$last = strtolower($val{strlen($val)-1});
switch($last)

{
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}


function upload_max_filesize()
{
return return_bytes(ini_get(\'upload_max_filesize\'));
}



No comments: