PDF上传不起作用,但其他扩展程序有效

时间:2012-11-13 10:55:00

标签: php jquery codeigniter file-upload

我有一个用来接受用户图片的程序。我改为接受PDF文件而不是客户要求的问题是它不起作用。

我已将mimes.php配置更改为此

'pdf'    =>    array('application/pdf', 'application/x-pdf', 'application/x-download','application/download','binary/octet-stream'),

这是我保存上传文件的CI代码

$config = array(
'upload_path'   =>  'news',
'allowed_types' =>  'pdf',
'max_size'  =>  '10000000',
);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = $this->upload->data();
echo json_encode(array('data' => $data));

HTML

<form id="imgadd-form" method="post" accept-charset="utf-8" enctype="multipart/form-data">
   Upload :<br>
   <input type="file" name="userfile" id="userfile">
</form>

Jquery的

function uploadfile()
{
$.ajaxFileUpload({
    url     :   'save_data/uploadfile/',
    secureuri   :   false ,
    fileElementId   :   'userfile' ,
    dataType    :   'json' ,
    success     :   function( data , status ) {
        if ( status != 'error' ){//&& data.data.is_image ) {
            sysAlert( 'File Successfully Uploaded' );
        }
        else
            sysAlert( 'Error Uploading' );
    }
});
}

编辑: jquery报告文件成功上传,因为它返回JSON,但每当我检查目录时,都没有。 奇怪的是,我将文件扩展名重命名为.TXT并上传了3.5MB pdf文件,扩展名为.txt,并且上传成功并位于正确的目录中。

编辑: var_dump $ _FILES

"array(1) {
  ["userfile"]=>
  array(5) {
    ["name"]=> string(8) "test.pdf"
    ["type"]=> string(24) "application/octet-stream"
    ["tmp_name"]=>string(24) "C:\xampp\tmp\phpF84F.tmp"
    ["error"]=>int(0)
    ["size"]=>int(3748991)
  }
}

echo $ this-&gt; upload-&gt; display_errors();

<p>The filetype you are attempting to upload is not allowed.</p>

更新: 更改'max_size'

'max_size'      =>  '10000000',

CI版本为2.1.2

1 个答案:

答案 0 :(得分:1)

Mimes.php

'pdf'   =>  array('application/pdf', 'application/x-download')

CI代码:

$config['upload_path']   = '$path'; 
                $config['allowed_types'] = 'pdf';   
                $config['max_size']      = '4096';      
                $config['overwrite']     =  TRUE;

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

                if (!$this->upload->do_upload("csv_file")) {
                      echo $this->upload->display_errors(); die();
                      $this->data['error'] = array('error' => $this->upload->display_errors());
                } else {
                    $upload_result = $this->upload->data(); 
                }