2017-08-14 64 views
1

通常情況下,我們可以使用$this->input->get('field_name')$this->input->post('field_name')來獲取表格數據Codeigniter,這很好。Codeigniter上傳文件名

在原始PHP中,我們使用$_FILES["fileToUpload"]["name"]來獲取用戶嘗試上傳的文件名。

我的問題是:有沒有Codeigniter的方法來獲取需要上傳的文件的名稱?

我想說,我需要獲取用戶試圖上傳的文件名,然後嘗試使用Codeigniter庫而不是使用原始PHP全局$_FILES變量將其保存在我的服務器中。

<?php 

class Upload extends CI_Controller { 

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

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

     public function do_upload() 
     { 
       $config['upload_path']   = './uploads/'; 
       $config['allowed_types']  = 'gif|jpg|png'; 
       $config['max_size']    = 100; 
       $config['max_width']   = 1024; 
       $config['max_height']   = 768; 

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

       // get the user submitted file name here 

       if (! $this->upload->do_upload('userfile')) 
       { 
         $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); 
       } 
     } 
} 
?> 

回答

0
​​

這裏是2版本的文檔是23

,如果你想在後臺文件名:

$this->upload->file_name它的工作基礎上system/library/upload.php 此功能。

public function data() 
{ 
    return array (
        'file_name'   => $this->file_name, 
        'file_type'   => $this->file_type, 
        ... 
       ); 
} 

如果需要獲取文件名...

之前保存到服務器...你有工作在JavaScript

<?php echo "<input type='file' name='userfile' size='20' onchange='changeEventHandler(event);' />"; ?>

onchange事件在javascript:

<script> 
function changeEventHandler(event){ 
    alert(event.target.value); 
} 
</script> 
+0

在調用'$ this-> upload-> do_upload('userfile')'之前,我可以調用'$ this-> upload-> data()'嗎? –

+0

我編輯了我的答案@MahbubulIslam ...您是否期​​望在提交按鈕之前獲取文件名?或提交表格後 – Nawin

+0

我想說我需要知道用戶試圖上傳之前試圖上傳它在我的服務器上的文件名。 –