CIはファイルをアップロードしてアップロードしたパスを出力する

2845 ワード

1、フォームの作成upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>

<form action="<?php echo site_url('upload/do_upload') ?>"  method="post" class="form-horizontal" enctype="multipart/form-data">



<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />


</form>



</body>
</html>

2、アップロードプログラムを書く.php
<?php
/*constructor function to initialize controller and load the file upload class,
 plus the two other helpers it needs */
class Upload extends CI_Controller {
    function Upload()
    {
          //parent::Controller();
      parent::__construct(
        );  
       
         

    }
    /*now the function which does all the work!*/
    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);
      if(! $this->upload->do_upload()){
        
          $error = array('error' => $this->upload->display_errors());         
          $this->load->view('upload_form_error', $error);

      }else{
         $data = array('upload_data' => $this->upload->data());
         $this->load->view('upload_success', $data);

      }
      

      // $this->upload->do_upload();      
      // $data = array('upload_data' => $this->upload->data());
      // $this->load->view('upload_success', $data);

    }
}

3、呼び出しwelcome入口コントローラ呼び出しビューupload_form
public function index()
	{
		// $this->load->view('header');
		// $this->load->view('index');
		// $this->load->view('footer');
		$this->load->view('upload_form');
	}

4、成功したページをアップロードし、アップロードしたファイルパスを出力するupload_success.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>

<h3>Your file was successfully uploaded!</h3>



<ul>
<?php foreach($upload_data as $item => $value):?>
<?php if($item=='full_path'){
	echo $value;
} 
	
?>


<?php endforeach; ?>
</ul>

<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>

</body>
</html>

5、アップロードに失敗した表示ページupload_form_error.php