codeigniter txt文件上传

时间:2013-03-18 13:24:19

标签: codeigniter file-upload

我正在使用codeigniter,当我尝试上传一些文本文件时,我发现了一个奇怪的案例。

使用指示(CI doc)的方法文档一切正常,但如果我尝试上传文件包含字符串'//'(不带引号),我得到的结果是“您尝试的文件类型不允许上传。“

有什么想法吗?

使用的代码:(控制器)

function do_upload() {
    $config['upload_path'] = APPPATH.'uploads/';
    $config['allowed_types'] = 'txt|csv';
    $config['max_size'] = '1024';
    $config['max_width']  = '0';
    $config['max_height']  = '0';
    $this->load->library('upload', $config);
    @var_dump($_FILES['userfile']);   // debug
    if (!$this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    } else 
        echo "<br>upload ok";
}

upload_form.php(查看):

<?php 
    if (isset($error)) echo $error;
    echo form_open_multipart('supervisor/do_upload'); 
    echo form_label('File', 'userfile');
    echo form_upload('userfile');
    echo form_submit('submit', 'Upload');
    echo form_close() 
?>

例如:( .txt for upload)

[file1.txt]:
THIS IS A TEXT FILE     # work fine

[file2.txt]:
THIS IS A // TEXT FILE    # fail!  note: {blank}//{blank}

输出(上传file1.txt)

array(5) { ["name"]=> string(9) "file1.txt" ["type"]=> string(10) "text/plain" ["tmp_name"]=> string(14) "/tmp/phpLVaMGs" ["error"]=> int(0) ["size"]=> int(19) }
upload ok

输出(上传file2.txt)

array(5) { ["name"]=> string(9) "file2.txt" ["type"]=> string(10) "text/plain" ["tmp_name"]=> string(14) "/tmp/phpFu4znR" ["error"]=> int(0) ["size"]=> int(22) }

The filetype you are attempting to upload is not allowed.
...

Firebug POST :(上传file2.txt)

userfile    THIS IS A // TEXT FILE
submit  Upload
Fuente
-----------------------------15028408581691128039842883428 
Content-Disposition: form-data; name="userfile"; filename="file2.txt" 
Content-Type: text/plain 
THIS IS A // TEXT FILE 
-----------------------------15028408581691128039842883428 
Content-Disposition: form-data; name="submit" 
Upload 
-----------------------------15028408581691128039842883428--

1 个答案:

答案 0 :(得分:0)

我多次遇到这个问题,并且在使用新的系统文件夹时我总是会做以下修复。这是一个CI错误。请让我知道它的作品不是???

转到系统/ libraries / Uploads.php - 行号1059

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

已更改为:

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

现在看行号1044-1045 。它应该看起来像:

$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;