请看下面的片段。我正在使用它来限制超过 5 mb 的文件。每当文件超过 5 mb 时,它就会说“不允许您尝试上传的文件”,而实际上它应该说“您尝试上传的文件太大”。并不是我将它们放在代码的错误部分,我正在使用:
if(filesize($_FILES['filename']['tmp_name']) > $max_filesize)
完整代码:
// Configuration - Your Options
$allowed_filetypes = array('.pdf','.jpg','.png','.gif');
$max_filesize = 5242880; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = "/store/user/$user";
$filename = $_FILES['filename']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['filename']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('');