2017-08-14 4 views
1

通常、または$this->input->post('field_name')を使用してフォームデータをCodeigniterに取得できます。Codeigniterアップロードファイル名

PHPでは、ユーザーがアップロードしようとしているファイル名を取得するのに$_FILES["fileToUpload"]["name"]を使用します。

私の質問です:Codeigniterアップロードする必要があるファイルの名前を取得する方法はありますか?

私は、PHPグローバル$_FILESの変数を使用する代わりに、Codeigniterライブラリを使用して自分のサーバーに保存しようとする前に、アップロードしようとしているファイル名を取得する必要があると言います。ここで

<?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バージョンのドキュメントは、2ためと3

ためです:

$this->upload->file_nameそれはsystem/library/upload.php これに基づいて動作します関数。

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

ファイル名を取得する必要がある場合は...

サーバに保存する前に...あなたは仕事を持っているのjavascriptはJavaScript

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

のonchangeイベントで:

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

'$ this-> upload-> do_upload( 'userfile')'を呼び出す前に '$ this-> upload-> data()'を呼び出せますか? –

+0

私の回答を編集しました@MahbubulIslam ...ボタンを送信する前にファイル名を取得する予定ですか?またはフォームを提出した後 – Nawin

+0

私は自分のサーバーにアップロードしようとする前にアップロードしようとしているファイル名を知っておく必要があると言おうとしています。 –

関連する問題