我正在使用Uploadify来处理我的CakePHP应用程序中的上传。一些上传工作正常
这是我的javascript代码:
<script type="text/javascript">
$(document).ready(function() {
$('.submit input').attr('disabled','disabled');
$('#uploadify').uploadify({
'uploader' : '/uploadify/uploadify.swf',
'script' : '/videos/ajaxUpload',
'cancelImg' : '/uploadify/cancel.png',
'folder' : '/files/video',
'auto' : true,
'multi' : true,
'sizLimit' : 31457280,
'onComplete': function(event,id,fileObj,response,data){
console.log(fileObj);
var responseObj = $.parseJSON(response);
console.log(responseObj);
$('#upload-complete').html(responseObj.message);
$('#VideoName').val(responseObj.name);
$('.submit input').attr('disabled',false);
},
'buttonText': 'CHOOSE FILE',
'fileExt' : '*.mp4;*.mpg;*.mpeg;*.mov;*.avi;*.mpv2;*.qt;*.flv;'
});
});
</script>
这是处理文件上传的控制器代码:
public function ajaxUpload(){
$this->autoRender = false;
$name = $type = $size = $status = false;
$message = 'There was a problem uploading the file';
if (!empty($_FILES)) {
if ($_FILES['Filedata']['error'] == 0){
$allowedTypes = array(
'mp4',
'mpg',
'mpeg',
'mov',
'avi',
'mpv2',
'qt',
'flv'
);
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$allowedTypes)){
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = WWW_ROOT . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
$name = array_pop(explode('/',$targetFile));
$type = $_FILES['Filedata']['type'];
$size = $_FILES['Filedata']['size'];
$status = 1;
$message = 'File successfully uploaded';
}else{
$status = 0;
$message = 'Invalid file type.';
}
}else{
$status = 0;
switch($_FILES['Filedata']['error']){
case 1:
$message = 'File exceeded max filesize';
break;
case 2:
$message = 'File exceeded max filesize';
break;
case 3:
$message = 'File only partially uploaded';
break;
case 4:
$message = 'No file was uploaded';
break;
case 7:
$message = 'There was a problem saving the file';
break;
default:
$message = 'There was a problem uploading the file';
break;
}
}
}else{
$status = 0;
$message = 'No file data received.';
}
echo json_encode(
array(
'status'=>$status,
'name'=>$name,
'type'=>$type,
'size'=>$size,
'message'=>$message
)
);
}
对于小于8MB的文件,这一切都像是一个魅力,但对于超过该大小的文件,控制器会显示“没有收到文件数据。”,表示$ _FILES为空。这很奇怪 - 如果文件超出了php.ini中的某些指令,我会预料到其中一个错误。
有人可以帮忙吗?
答案 0 :(得分:3)
问题是post_max_size
ini指令,默认设置为8MB。如果上传的文件超过此值,则不会引发错误,只会导致所有超级全局(例如$_FILES
和$_POST
)为空。
它还会向日志文件输出警告。但没有标准输出。
您无法直接检测是否超出post_max_size
。你只能根据你对超级球的期望与你得到的结果进行猜测。
另一方面,您可以通过检查upload_max_filesize
错误来以编程方式检测是否超出$_FILES['userfile']['error']
。