I'm using Uploadify to handle uploads in my CakePHP app. Some uploads work fine
Here's my javascript code:
<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 = $.pars开发者_运维技巧eJSON(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>
And here's the controller code that deals with the file uploads:
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
)
);
}
This all works like a charm for files smaller than around 8MB, but for files over that size, the controller says "No file data received.", indicating that $_FILES is empty. This is odd - I would have expected one of the other errors if the file exceeded some directive in php.ini.
Can anyone help?
The problem is the post_max_size
ini directive, by default set to 8MB. If an uploaded file exceeds this value, it doesn't throw an error, it just results in all superglobals (e.g. $_FILES
and $_POST
) being empty.
It also prints a warning to the log file. But nothing to standard output.
You can't directly detect if post_max_size
was exceeded. You can only guess based on what you were expecting in the superglobals vs what you got.
On the other hand, you can programatically detect if upload_max_filesize
was exceeded by checking $_FILES['userfile']['error']
for errors.
精彩评论