我有一個upload.php腳本,我從移動設備訪問,以上傳文檔。PHP的上傳和下載不適用於較大的文件(> 1 MB)
每當我上傳某種圖像,即< 1MB,它可以很好地工作,但是,當上傳一個比這個大的文件時,它會被損壞。
上傳腳本也重命名文件,並從中刪除擴展,如果這可能有什麼關係的錯誤...
這裏的腳本:
<?php header('Access-Control-Allow-Origin: *');
//Read parameters - I use this when I'm adding the file to the database.
$documentuniqueid = addslashes($_REQUEST['documentuniqueid']);
$type = addslashes($_REQUEST['type']);
$notes = addslashes($_REQUEST['notes']);
//Get file name
$filename = urldecode($_FILES["file"]["name"]);
// Check for errors
if($_FILES['file']['error'] > 0){
outputJSON('An error ocurred when uploading.');
}
// Check filetype
//if($_FILES['file']['type'] != 'image/png'){
// outputJSON('Unsupported filetype uploaded.');
//}
// Check filesize
if($_FILES['file']['size'] > 500000){
outputJSON('File uploaded exceeds maximum upload size.');
}
// Check if the file exists
if(file_exists('upload/' . $_FILES['file']['name'])){
outputJSON('File with that name already exists.');
}
// Upload file
if(!move_uploaded_file($_FILES['file']['tmp_name'], 'documents/'.$documentuniqueid)){
outputJSON('Error uploading file - check destination is writeable.');
}
?>
下載腳本如下像這樣:
<?php
sleep(2);
$document_id = addslashes($_REQUEST['document_id']);
if($document_id != "") {
$real_document_name = GetRealFileName($document_id);
if($real_document_name != "ERROR") {
$original_filename = "http://www.whatever.com/documents/".$document_id;
$new_filename = $real_document_name;
//Headers
$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('Content-Type: '.finfo_file($finfo, $original_filename));
header("Content-Length: " . filesize($original_filename));
header('Content-disposition: attachment; filename="'.$new_filename.'"');
//clean up
ob_clean();
flush();
readfile($original_filename);
exit();
}
}
?>
有關改進這方面的任何提示? 或者任何見解,爲什麼這是不正確的? 你可以看到我在上傳時將文件重命名爲一個隨機字符串,在下載時將它們重命名,然後查找文件名並將其重命名爲原始文件名。
對小文件大小的預期工作。
我還必須注意,即使我手動進入FTP,下載上傳的文件並自己添加正確的擴展名,我無法打開它們。圖像看起來很糟糕,例如PDF文件已損壞。
PHP詳情:
兩個upload_tmp_dirpost_max_size以及upload_max_filesize的設置爲100M。 max_file_uploads設置爲20和file_uploads爲ON max_input_time設置:60 的max_execution_time:3000
運行的phpinfo(),並檢查您的設置...如的post_max_size ...閱讀更多http://stackoverflow.com/questions/7754133/php-post-max-size- overrides-upload-max-filesize – donald123
嗨唐納德! post_max_size是100M。我認爲這不是問題,應該是我在代碼中忽略的東西。 – Laureant
嘗試通過FTP上傳,然後檢查您是否看到任何錯誤消息 –