【问题标题】:Code Igniter File Upload - No File TypeCodeigniter 文件上传 - 无文件类型
【发布时间】:2012-09-05 15:49:17
【问题描述】:

在过去的一个小时里,我一直在 Code Igniter 论坛上搜索这个问题:

我正在使用 Code Igniter 为 Web 应用程序编写文件上传处理程序。到目前为止,我有以下代码来处理上传:

public function send() {        
    $config = array(
        'upload_path' => 'path/to/my/upload/directory',
        'allowed_types' => 'pdf'
    );

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

    $this->upload->do_upload('pdf_upload');

    echo "<pre>";
    print_r($this->upload->data());
    echo "</pre>";
    exit();
}

我的看法:

<?= $errors ?> <br />
<?= form_open_multipart('/Upload_test/send') ?>
    <p><label for="pdf_upload">File: (PDF ONLY)</label> <input type="file" name="pdf_upload" id="pdf_upload" /></p>
    <p><input type="submit" /></p>
</form>

当我提交表单并选择了有效的 PDF 文件时,我从 print_r() 获得以下输出:

Array
(
    [file_name] => my_file.pdf
    [file_type] => 
    [file_path] => path/to/my/upload/directory/
    [full_path] => path/to/my/upload/directory/my_file.pdf
    [raw_name] => my_file
    [orig_name] => 
    [client_name] => my_file.pdf
    [file_ext] => .pdf
    [file_size] => 4190
    [is_image] => 
    [image_width] => 
    [image_height] => 
    [image_type] => 
    [image_size_str] => 
)

文件类型为空白。可能是什么原因造成的?我错过了什么?

【问题讨论】:

  • 你运行的是什么版本的 CI?
  • 我看到你找到了答案。我打算提出其他问题,但不确定它是否适用。
  • @nageeb 是的,我不确定这是一个重复的问题。很明显,dangermark(该问题的提问者)在上传时正在获取文件类型;我不是。令人高兴的是,他的问题的答案也是我的答案。 :-)

标签: php codeigniter file-upload


【解决方案1】:

我在一个相关但不重复的问题中找到了答案:Uploading in Codeigniter - The filetype you are attempting to upload is not allowed

如果您使用的是 Codeigniter 2.1.0 版,则上传中存在错误 图书馆。更多信息请见http://codeigniter.com/forums/viewthread/204725/ 详情。

基本上我所做的就是修改文件上传中的几行代码 类(位置:./system/libraries/Upload.php)

1) 修改1044行号

来自:

$this->file_type = @mime_content_type($file['tmp_name']);
return;

到这里:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 

2) 修改行号1058

来自:

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_path']), $output, $return_code);

到这里:

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code); 

正如您可能看到的,第 1058 行尝试使用一个数组值 不存在。

【讨论】:

  • 很高兴您修复了它,但您应该扩展核心库而不是直接编辑它。只需将您编辑的方法从核心库复制到新库中,这样,如果您更新 CI 核心,您的更改就不会中断。
猜你喜欢
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 2015-06-04
  • 2013-04-04
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
相关资源
最近更新 更多