2016-01-20 95 views
0

我想接受表格上的PDF和图像。 我这样编写名为'Myfileupload'的自定义类。codeigniter图像和pdf上传失败

<?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"" } 
+0

请到'配置/ mimes.php'并用 ' 'PDF' \t => \t阵列( '应用/ PDF',“应用程序/力下载替换'pdf'值','application/x-download','binary/octet-stream'),'。 似乎在任何浏览器中为我工作 – LefterisL

+0

谢谢。 @TerisL。我已经做到了。我发现这不是问题的原因。 – AMS

回答

0
$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(); 
}