請參閱下面的代碼片段。我以一種限制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('');
上傳一個_less_然後是.5MB的文件會發生什麼? – JakeParis
最好的方法應該是檢查客戶端文件的大小(但我不知道如何,從谷歌一些js :-))。在服務器端,你可以簡單地設置限制ini_set/upload_max_filesize並檢查錯誤... – Fanda
@Fanda,我敢肯定,嘗試限制'ini_set'的上傳大小不能很好地工作。 – JakeParis