【发布时间】:2017-04-14 15:51:54
【问题描述】:
我有一个 upload.php 脚本,我可以从移动设备访问它,用于上传文档。
每当我上传某种小于 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 详细信息:
post_max_size 和 upload_max_filesize 都设置为 100M。 max_file_uploads 设置为 20 并且 file_uploads 为 ON 最大输入时间:60 max_execution_time : 3000
【问题讨论】:
-
运行 phpinfo() 并检查您的设置...例如 post_max_size ...阅读更多stackoverflow.com/questions/7754133/…
-
嗨,唐纳德! post_max_size 为 100M。我不认为这是这里的问题,它应该是我在代码中忽略的东西。
-
尝试通过 FTP 上传,然后检查您是否看到任何错误消息
-
和upload_max_filesize?
-
post_max_size 和 upload_max_filesize 都设置为 100M。 max_file_uploads 设置为 20 且 file_uploads 为 ON
标签: php file-upload upload