【问题标题】:PDF upload not working but other extensions worksPDF 上传不工作,但其他扩展工作
【发布时间】:2012-11-01 19:23:22
【问题描述】:

我有一个用于接受用户图像的程序。我根据客户的要求将其更改为接受 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 并上传了 .txt 扩展名的 3.5MB pdf 文件,它成功上传并位于正确的目录中。

编辑: $_FILES的var_dump

"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->upload->display_errors();

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

更新: 更改“最大尺寸”

'max_size'      =>  '10000000',

CI 版本为 2.1.2

【问题讨论】:

  • 你确定你设置了足够的upload_max_filesizepost_max_size吗?
  • @Serg 我检查了 phpinfo(); upload_max_filesize 128Mpost_max_size 8M
  • 你能检查 $_FILE 变量做一个 var_dump 并查看它的内容,也许你能找到一些缺失的东西!
  • 还尝试将 application/octet-stream 添加到数组 'pdf' => array('application/pdf', 'application/x-pdf', 'application/x-download',' application/download','binary/octet-stream'), 在 mimes.php
  • 很高兴它对您有所帮助,随时欢迎 :)

标签: php jquery codeigniter file-upload


【解决方案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(); 
                }

【讨论】:

  • 奇怪的是只有firefox认为pdf文件是'application/x-download'格式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
相关资源
最近更新 更多