php - 将 Codeigniter 的上传库与 Uploadify 结合使用 (Uploadifive)

标签 php ajax codeigniter jquery uploadify

我有一个“文档”上传表单,我已经通过正常的 Codeigniter upload library 运行了它。 。但是,我需要它作为 Ajax 调用。我有一个 Uploadifive 脚本(我购买了),它的 HTML5 相当于 Uploadify .

我的问题是无法让 Uploadifive jQuery Ajax 插件与我的 Controller 功能配合良好。没有控制台错误、CI 错误,也没有 ajax 函数输出错误。事实上,Uploadify 函数似乎没有起作用,因为页面重定向到我的 Controller 函数并返回 $error = array('error' => $this->upload->display_errors()) ; 下面 Controller 函数的第一部分出现错误。

有人有将 Uploadifive 与 Codeigniter 结合使用的经验吗?我的代码有什么突出的地方吗?

请注意,我正在加载所有正确的脚本、库、模型等。此外,尚未对表单验证进行编码。同样,无需 Uploadify 插件一切正常

Controller 功能(即 upload/add_document):

function add_document() {

    if (!$this->upload->do_upload()) {

        // If file upload failed or is invalid, 
        // display error notification
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload', $error);
    }       
    else {

        // If file upload was successful,
        // get 'file_name' of the uploaded document
        $file_uploaded = $this->upload->data();
        $file_name = $file_uploaded['file_name'];

        // Add document to S3
        $query = $this->s3_model->add_document($file_name);
        if($query) {           

            // If successful, return S3 file url 
            $document_url = $query;

            // Add document and form details to db
            $this->content_model->add_doc($document_url);

            // Delete temporary document from local 'uploads' folder
            unlink('uploads/'.$file_name);
        }                   

        $this->load->view('upload');
    }

}

表格:

<? echo form_open_multipart('upload/add_document', 'id="upload-doc-form"') ;?>
    <? echo form_input('title', '', 'placeholder="Title"') ;?><br/>
    <? echo form_textarea('description', '', 'placeholder="Description"') ;?><br/>
    <? echo form_input('company',  '', 'placeholder="Company"') ;?><br/>
    <? echo form_input('company_url', '', 'placeholder="Company URL"') ;?><br/>
    <? echo form_upload('userfile', '', 'id="file-upload"') ;?>
    <br/><br/>
    <? echo form_submit('', 'Upload', 'class="doc-submit"') ;?>
<? echo form_close() ;?>

JS(封装在 $(function(){})

var form = $('#upload-doc-form');

$('#file-upload').uploadifive({                 
    'auto': true,
    'buttonText': 'BROWSE',
    'uploadScript': form.attr('action'),
    'onError': function(errorType) {
        alert('The error was: ' + errorType);
    }
});

这是 Uploadify 中包含的DEFAULT打包的服务器端脚本,我也没有指出(obvs)。这只是为了向您展示 Uploadify 如何处理数据。我改为使用 Controller ...我认为这是这里的问题。

<?php
/*
UploadiFive
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
*/

// Set the uplaod directory
$uploadDir = '/uploads/';

// Set the allowed file extensions
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    $tempFile   = $_FILES['Filedata']['tmp_name'];
    $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
    $targetFile = $uploadDir . $_FILES['Filedata']['name'];

    // Validate the filetype
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

        // Save the file
        move_uploaded_file($tempFile, $targetFile);
        echo 1;

    } else {

        // The file type wasn't allowed
        echo 'Invalid file type.';

    }
}
?>

最佳答案

问题是我没有设置(或更改)从 Ajax 调用发送到我的 Controller 的文件对象名称。 Codeigniter 的 UPLOAD 库查找名为 userfile 的文件对象。默认情况下,Uploadifive 发送数据字符串 fileData

非常简单的编辑 - 我应该捕获的东西......

在 Uploadifive Alax 函数中,我必须重命名 fileObjName 以与 CI 上传库一致。就像这样:

$(function() {
    $('#file-upload').uploadifive({
        'fileObjName': 'userfile',
        ...
    });
}); 

关于php - 将 Codeigniter 的上传库与 Uploadify 结合使用 (Uploadifive),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390859/

相关文章:

php - Codeigniter 按钮重定向

php - 如何在变量参数函数中使用查询?

php - 是否以尾部斜杠 (/) 结束我的网址?我该如何使用 codeigniter 来做到这一点

php - 在 codeigniter 中扩展多个核心 Controller

javascript - 将 Angular 添加到现有的 CodeIgniter 项目

php - MYSQL无法更新

php - 在 VS 2010 中启用 PHP 亮点?

javascript - 动态创建的 Chart.js 图表基于时间的 x 轴填充过多?

jquery - 将 javascript 函数放入 '$()' 中有什么作用?

css - 更新进度面板、CSS 和删除内联样式