2013-05-20 97 views
0

使用codeigniter我已經上傳了一些文件,但我無法讀取這些文件。無法讀取在COdeigniter上傳文件夾中上傳的文件

<?php 
if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Upload extends CI_Controller { 

    /** 
    * file Info 
    * @access priavte 
    * @var object 
    */ 
    private $uploadFileInfo; 

    private $galary_path_url; 

    /** 
    * load the form and url helper library 
    * load our new PHPExcel library 
    * contuctor 
    */ 
    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 

     $this->load->library('excel'); 

     $this->galary_path_url = base_url() . 'upload/'; 
    } 

    /** 
    * load the upload form by defeault 
    * @access public 
    * @return void 
    */ 
    function index() 
    { 
     $this->load->view('/upload/upload_form', array('error' => ' ')); 
    } 

    /** 
    * does the upload 
    * @access public 
    * @return void 
    */ 
    function do_upload() 
    { 
     //set up the config file 
     //only doc,docx,xls,xlsx files can be uploaded 
    $config['upload_path'] = './upload/'; 
    $config['allowed_types'] = '|doc|docx|xls|xlsx'; 
    $config['max_size'] = '2000'; 

    //load the upload libraby with current config file 
     $this->load->library('upload', $config); 

    //if not uploaded 
     if (! $this->upload->do_upload()) 
    { 
      echo $_FILES['userfile']['type'] ; 
      $error = array('error' => $this->upload->display_errors()); 

      $this->load->view('/upload/upload_form', $error); 
     } 
     //uploaded successfully. 
     else 
    { 
      $this->uploadFileInfo = $data = array('upload_data' => $this->upload->data()); 
      $this->load->view('/upload/upload_success', $data); 
      $this->pdfExport(); 
    }   
    } 

    /** 
    * detect the files and send to the right method 
    * use the convert this to pdf file. 
    * @access public 
    * @return void Description 
    */ 
    public function pdfExport() 
    { 
     echo $filePath = $this->galary_path_url . $this->uploadFileInfo['upload_data']['file_name']; 

      var_dump(file_exists($filePath)); 


    } 

} 
?> 

但是FILE_EXITS SHOWS FALSE。在此先感謝

+0

月1日,因爲他們是accessable爲用戶的網址.. 你是怎麼從回聲$文件路徑獲得不創建控制器內的公共職能? – Svetoslav

+0

我爲測試目的寫了公共函數。我必須測試它。那麼它將是私人的。功能。此外問題已經解決。 :D – nixon1333

回答

0

獲取絕對文件名,you may want to use full_path instead of file_name。這將爲您提供文件的絕對文件系統路徑,而無需自己創建路徑。目前,您將base_url連接到文件名,它不會給你一個文件路徑,它會給你一個URL

替換;

echo $filePath = $this->galary_path_url . 
       $this->uploadFileInfo['upload_data']['file_name']; 
var_dump(file_exists($filePath)); 

with;

$fullPath = $this->uploadFileInfo['upload_data']['full_path']; 
echo $fullPath; 
var_dump(file_exists($fullPath)); 
+0

謝謝。我可以使用此路徑將該文件加載到另一個類中。喜歡閱讀文件?對不起,如果我問一個愚蠢的問題。 – nixon1333

+0

@ nixon1333是的,只要類預計文件名,這應該工作得很好:) –

+0

實際上,我需要從該文件讀取的文件。就像我上傳一個xls文件,然後閱讀該文件表單,將其導出爲pdf。基本上我需要從該文件中讀取該文件。 – nixon1333