【问题标题】:AJAX file input image upload in CakephpCakephp中的AJAX文件输入图片上传
【发布时间】:2012-01-18 14:28:19
【问题描述】:

我正在通过 AJAX ajaxFileUpload 插件上传图像文件,该插件使用 iframe 提交文件。我已经成功地将文件上传到我的控制器,我可以看到 tmp_name、name、error = 0 等,但是当我使用这个 $this->data['Card']['tmp_name'] 时,无论使用 move_uploaded_file,它总是返回 false路径是否正确...从现在开始我不确定。

以下是我目前用于查看文件的代码...

function ajaxFileUpload() {
    $.ajaxFileUpload({
        url: '/cards/ajaxFrontCardUpload',
        secureuri: false,
        fileElementId: 'CardUploadFront',
        dataType: 'json',
        success: function (data, status) {
            console.log(data);
            $('#uploadFrontImage').attr('src', data.tmp_path);
        },
        error: function (data, status, e) {
            alert(e);
        }
    })
    return false;
}

$('#CardUploadFront').live('change', function () {
    ajaxFileUpload();
});

echo $form->file('Card.uploadFront', array('class'=>'file'));

下面是控制器功能:

public function ajaxFrontCardUpload() {
        $this->layout = 'ajax';
        $tmp_name = $this->data['Card']['uploadFront']['tmp_name'];
        $tmp_name = $this->data['Card']['uploadFront']['tmp_name'].'/'.$this->data['Card']['uploadFront']['name'];
        $json_response['tmp_path'] = '/img/cards/temp/'.time().'.png';
        if(move_uploaded_file($tmp_name, $json_response['tmp_path'])){
            $json_response['response'] = 'true';
        }else{
            $json_response['response'] = 'false';
        }
        $this->set(compact('json_response'));
    }

各位有什么想法吗?

【问题讨论】:

    标签: php jquery ajax cakephp


    【解决方案1】:

    问题出在这里:

    public function ajaxFrontCardUpload() {
            $this->layout = 'ajax';
            $tmp_name = $this->data['Card']['uploadFront']['tmp_name'];
            $tmp_name = $this->data['Card']['uploadFront']['tmp_name'].'/'.$this->data['Card']['uploadFront']['name']; 
    //notice here that $tmp_name now no longer references the path to the uploaded file
            $json_response['tmp_path'] = '/img/cards/temp/'.time().'.png';
            if(move_uploaded_file($tmp_name, $json_response['tmp_path'])){
                $json_response['response'] = 'true';
            }else{
                $json_response['response'] = 'false';
            }
            $this->set(compact('json_response'));
        }
    

    上传文件的路径存储在$this->data['Card']['uploadFrom']['tmp_name']。 当您将'/'.$this->data['Card']['uploadFront']['name'] 附加到它时,您的$tmp_name 变量不再指向上传的文件。这就是 move_uploaded_file 返回 false 的原因。

    【讨论】:

      猜你喜欢
      • 2011-10-06
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 2012-06-11
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多