【问题标题】:codeigniter image and pdf upload failcodeigniter 图像和 pdf 上传失败
【发布时间】:2016-01-20 15:51:03
【问题描述】:

我想接受表单上的 pdf 和图像。 我像这样编写名为“Myfileupload”的自定义类。

<?php 
    defined('BASEPATH') OR eixt('No direct script acccess allowed');
    //myfileupload library

    class Myfileupload{
        var $image_name;
        var $pdf_name;

        function __construct() {
            $this->CI = & get_instance();
        }

        function upload_image($image) {
            $config['upload_path'] = './upload/image';
            $config['allowed_types'] = 'jpeg|gif|jpg|png';
            $config['max_size'] = 0;
            $config['max_width']  = 0;
            $config['max_height']  = 0;
            $config['encrypt_name'] = TRUE;

            $this->CI->load->library('upload', $config);
            $temp = $this->CI->upload->do_upload($image);
            $this->image_name = $this->CI->upload->data('file_name');

            return $temp;
        }

        function getImageName() {
            return $this->image_name;
        }

        function upload_pdf($pdf_name) {

            $config['upload_path'] = './upload/pdf';
            $config['allowed_types'] = 'pdf';
            $config['max_size'] = 0;
            $config['encrypt_name'] = TRUE;

            $this->CI->load->library('upload', $config);
            $temp = $this->CI->upload->do_upload($pdf_name);
            $this->pdf_name = $this->CI->upload->data('file_name');
        }

        function getPdfName() {
            return $this->pdf_name;
        }
    }
?>

在控制器中,我首先调用“upload_image”函数。它完全符合我的预期。 之后我调用 upload_pdf 时发生错误。显示不允许上传“pdf”类型。 好像我不能用“pdf_upload”中的配置覆盖“image_upload”中的配置。

这是错误。结果用var_dump()修改;

PDF UPLOAD!
The filetype you are attempting to upload is not allowed.


array(14) { ["file_name"]=> string(94) "IP address သိရင္ ဘယ္လို hack လို႔ရႏိုင္သလဲ.pdf" ["file_type"]=> string(15) "application/pdf" ["file_path"]=> string(48) "/opt/lampp/htdocs/mm-bookstore.com/upload/image/" ["full_path"]=> string(142) "/opt/lampp/htdocs/mm-bookstore.com/upload/image/IP address သိရင္ ဘယ္လို hack လို႔ရႏိုင္သလဲ.pdf" ["raw_name"]=> string(90) "IP address သိရင္ ဘယ္လို hack လို႔ရႏိုင္သလဲ" ["orig_name"]=> string(14) "kali_linux.png" ["client_name"]=> string(94) "IP address သိရင္ ဘယ္လို hack လို႔ရႏိုင္သလဲ.pdf" ["file_ext"]=> string(4) ".pdf" ["file_size"]=> int(90637) ["is_image"]=> bool(false) ["image_width"]=> int(800) ["image_height"]=> int(557) ["image_type"]=> string(3) "png" ["image_size_str"]=> string(24) "width="800" height="557"" }

【问题讨论】:

  • 请转到config/mimes.php 并将pdf 值替换为'pdf' =&gt; array('application/pdf', 'application/force-download', 'application/x-download', 'binary/octet-stream'),。似乎在任何浏览器中都适用于我
  • 谢谢。 @TerisL。我已经这样做了。我发现这不是问题的原因。

标签: php codeigniter


【解决方案1】:
$CI =& get_instance();
header("Access-Control-Allow-Origin: *"); 
header('Access-Control-Allow-Methods: GET, POST');  

$config['upload_path'] = path;
$config['allowed_types'] = 'exe|psd|pdf|xls|ppt|php|rar|php4|php3|js|swf|Xhtml|zip|mid|midi|mp2|mp3|wav|bmp|gif|jpg|jpeg|png|html|htm|txt|rtf|mpeg|mpg|avi|doc|docx|xlsx';

$this->load->helper('form');

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

$this->upload->set_allowed_types('*');

if($this->upload->do_upload('file_name'))
{
$upload_data = $this->upload->data();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2020-06-12
    • 2013-05-02
    • 1970-01-01
    • 2016-11-26
    • 2012-07-17
    相关资源
    最近更新 更多