2013-11-01 21 views
1

我想要做的是在上傳文件後,將cvs文件解析爲print_r的屏幕,以便我可以使用它做一些事情。我似乎無法弄清楚如何使用$ data訪問文件名。我已經嘗試了幾個例如$ file_name,$ data ['file_name]和$ this-> data - > $ file_name。相關部分在線37.嘗試從上傳文件後從codeigniter獲取file_name

不知道我在做什麼錯。我已經查看了上傳器庫的代碼點火器文檔,但它不足以回答我的問題。謝謝你的幫助!

<?php 

class Upload extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 
    } 

    function index() 
    { 
     $this->load->view('upload_form', array('error' => ' ')); 
    } 

    function do_upload() 
    { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'csv'; 
     $config['max_size'] = '100'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

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

     if (! $this->upload->do_upload()) 
     { 
      $error = array('error' => $this->upload->display_errors()); 

      $this->load->view('upload_form', $error); 
     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 
      $this->load->view('upload_success', $data); 
       echo 'The file name is: '.$file_name; 
     } 
    } 
} 
?> 
+0

會發生什麼事,當你在你面前做的var_dump($數據)加載看法? –

+0

你可以簡單地改變這一行 - > $ data = array('upload_data'=> $ this-> upload-> data());到$ data = $ this-> upload-> data();然後訪問名稱$ data ['file_name']; – user2844810

回答

3

文件名可以是任何你想要的。這顯然是在文檔中,你可能已經讀錯頁或東西:

http://ellislab.com/codeigniter%20/user-guide/libraries/file_uploading.html

file_name: If set CodeIgniter will rename the uploaded file to this name. The extension provided in the file name must also be an allowed file type. 

OR:

您可以從$這個 - > upload->數據得到它(),這是一個數組。

$this->upload->data() 

This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype: 

Array 
(
    [file_name] => mypic.jpg 
    [file_type] => image/jpeg 
    [file_path] => /path/to/your/upload/ 
    [full_path] => /path/to/your/upload/jpg.jpg 
    [raw_name]  => mypic 
    [orig_name] => mypic.jpg 
    [client_name] => mypic.jpg 
    [file_ext]  => .jpg 
    [file_size] => 22.2 
    [is_image]  => 1 
    [image_width] => 800 
    [image_height] => 600 
    [image_type] => jpeg 
    [image_size_str] => width="800" height="200" 
) 
1

使用下面的代碼:

if($this->upload->do_upload($file)){ 
//Get uploaded file data here 
$allData=$this->upload->data(); 
$myFile = $allData['file_name']; 
} 
相關問題