如何从upload.php页面Codeigniter获取上传的文件名

时间:2016-04-17 21:39:59

标签: codeigniter variables upload controller

我有Upload.php页面如下=>

<?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|pgn'; 
         $config['max_size']      = 0; 
         //$config['max_width']     = 1024; 
         //$config['max_height']    = 768; 
          $config['detect_mime']    = TRUE;
         $this->load->library('upload', $config);

         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); 
         } 
      } 
   } 
?>

和Hesaplama.php控制器为=&gt;

    $upload_data = $this->upload->data(); //(line 51) Should return array of containing all of the data related to the file you uploaded.
    $file_name = $upload_data['file_name'];
    $file = fopen("<?php echo site_url('uploads/$file_name'); ?>", "r");

    while(! feof($file))
      {...

但是我收到以下错误=&gt;

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Hesaplama::$upload

Filename: controllers/Hesaplama.php

Line Number: 51

Backtrace:

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 51
Function: _error_handler

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 249
Function: pgn_oku

File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 28
Function: pozisyon_tutma

File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once

我们如何解决这个问题并获取新上传文件的文件名? 我个人认为该程序无法识别另一个页面(控制器)中的数据。 谢谢......

2 个答案:

答案 0 :(得分:0)

对不起我还不能评论,所以我在这里写。

我想库
$this->upload->data();

没有加载。

如果您只想获取文件名,也许您可​​以尝试使用会话。 在上传控制器:

$data = $this->upload->data();
$this->session->set_userdata('filename', $data['file_name']);

在Hesaplama:

$filename = 'uploads/'.$this->session->userdata('filename');
$file = fopen("<?php echo site_url('$filename'); ?>", "r");
希望这有帮助。

答案 1 :(得分:0)

获取文件名和扩展名文件以将其保存在数据库中,您可以使用tis

<?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|pgn'; 
         $config['max_size']        = 0; 
         $config['detect_mime']    = TRUE;

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

         if( ! $this->upload->do_upload('userfile')) {
            $error = array('error' => $this->upload->display_errors()); 
            $this->load->view('upload_form', $error); 
         }
         else { 
            $upload_data = $this->upload->data();

            /*
            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"
            )
            */

            $this->model->save_img_info($upload_data['file_name']);

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