2016-01-20 149 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(); 
}