【问题标题】:Uploadify & CakePHP - some uploads don't send any data to serverUploadify & CakePHP - 一些上传不会向服务器发送任何数据
【发布时间】:2011-11-17 19:54:07
【问题描述】:

我正在使用 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 中的某些指令,我会预料到其他错误之一。

谁能帮忙?

【问题讨论】:

    标签: jquery cakephp cakephp-1.3 uploadify


    【解决方案1】:

    问题在于 post_max_size ini 指令,默认设置为 8MB。如果上传的文件超过此值,它不会抛出错误,只会导致所有超全局变量(例如 $_FILES$_POST)为空。

    它还会向日志文件打印警告。但没有标准输出。

    您无法直接检测是否超出了post_max_size。您只能根据您对超全局变量的期望与您得到的结果进行猜测。

    另一方面,您可以通过检查 $_FILES['userfile']['error'] 的错误以编程方式检测是否超出了 upload_max_filesize

    【讨论】:

    • 嗨,Ben,感谢您的回答,但肯定在这种情况下 $_FILES 不会为空,因此服务器不会返回“未收到文件数据”,而是会返回“文件超出最大文件大小”?
    • 您使用的php版本是否低于4.2?在 4.2 之前,当超过 post_max_size 时,$_FILES 将完全为空,并且不包含任何错误消息。
    • 因为你说“大约 8mb”是问题发生的地方,所以症状与 post_max_size 完全匹配。我仍然相信这就是问题所在。
    • 另外,请检查您的日志文件中的警告。无论您的 php 版本如何,都会显示出来。
    • 谢谢 - 添加一个自定义 php.ini 和 post_max_size 50M 对我有用。不过,我使用的是 PHP 5.3 - 关于为什么 $_FILES 不包含错误(或其他任何内容)的任何想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    相关资源
    最近更新 更多