【问题标题】:Codeigniter file upload failingCodeigniter 文件上传失败
【发布时间】:2013-05-02 23:19:39
【问题描述】:

我有一个允许文件上传的表单。它可以很好地添加到数据库中,但不会将文件保存到指定的文件夹中。我的视图和模型代码如下(我的控制器只是检查文件是否已设置然后继续上传)

查看:

<form action="admin/admin_area/save_personnel" method="post" enctype="multipart/form-data" id="add_personnel">
    <input type="file" name="offshore_medical_certificate" />
</form>

控制器:

function uploadOffshoreMedical($uid)
{   
    $status = "";
    $msg = "";
    $file_element_name = 'offshore_medical_certificate';
    $certificate_name = 'Offshore Medical';
    if ($status != "error")
    {
        $config['upload_path'] = './certificate_files/';
        $config['allowed_types'] = 'pdf|doc|docx|txt|png|gif|jpg|jpeg|';
        $config['max_size']  = 1024 * 8;
        $config['encrypt_name'] = TRUE;

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($file_element_name))
        {
            $status = 'error';
            $msg = $this->upload->display_errors();
        }
        else
        {
            $data = $this->upload->data();
            $file_id = $this->saveCertificate($uid, $data['raw_name'], $data['file_ext'], $certificate_name);
        }
        if($file_id)
        {
            $status = "success";
            $msg = "File successfully uploaded";
        }
        else
        {
            unlink($data['full_path']);
            $status = "error";
            $msg = "Something went wrong when saving the file, please try again.";
        }

        @unlink($_FILES[$file_element_name]);
    }   
    echo json_encode(array('status' => $status, 'msg' => $msg));
}

我从表单中获得了正确的文件名,我上传的文件大小和文件扩展名都正确。我在本地主机上工作,所以文件夹权限很好,但由于某种原因它不起作用。我最近在另一个项目上做了类似的事情,但我似乎找不到问题。

任何帮助将不胜感激。

编辑:

如果我打印 $data 我得到以下内容

Array ( [file_name] => 08f8ce288e45207735bdb3f0c3139a0a.txt [file_type] => text/plain [file_path] => C:/wamp/www/marine/certificate_files/ [full_path] => C:/wamp/www/marine/files/08f8ce288e45207735bdb3f0c3139a0a.txt [raw_name] => 08f8ce288e45207735bdb3f0c3139a0a [orig_name] => license.txt [client_name] => license.txt [file_ext] => .txt [file_size] => 2.44 [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => ) 

{"status":"error","msg":"Something went wrong when saving the file, please try again."}

【问题讨论】:

  • 看起来$this-&gt;saveCertificate() 失败了;我们能看到吗?您要从中返回什么东西吗?
  • 正如stormdrain 所说,$this-&gt;saveCertificate() 失败了。尽管似乎正在发生的是 $this-&gt;upload-&gt;do_upload($file_element_name) 失败,但 $file_id 从未设置,因此 $this-&gt;saveCertificate() 失败。您的 $msg 变量也被覆盖,因此您看不到从 $this-&gt;upload-&gt;display_errors() 返回的错误;如果您删除第二个 if/else 语句 (if($file_id){...),您应该会看到一个错误,说明文件未上传的原因。 ' .

标签: codeigniter upload


【解决方案1】:

我建议你使用 Uploadify http://www.uploadify.com

它是一个非常易于使用的 jQuery 插件,带有令人惊叹的文档。我从开始编码就开始使用它,从未遇到任何问题。

这样您就可以轻松解决这两个项目的问题。

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 2013-10-08
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2012-03-18
    相关资源
    最近更新 更多