【问题标题】:Trying to get file_name from codeigniter after uploading上传后尝试从 codeigniter 获取 file_name
【发布时间】:2013-11-12 23:13:41
【问题描述】:

我要做的是在上传文件后使用 print_r 将 cvs 文件解析到屏幕上,这样我就可以用它做一些事情了。我似乎无法弄清楚您如何使用 $data.xml 访问文件名。我尝试了几种方法,例如 $file_name、$data['file_name] 和 $this->data->$file_name。相关部分在第 37 行。

不知道我做错了什么。我查看了上传器库的代码点火器文档,但这还不足以回答我的问题。感谢您的帮助!

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

【问题讨论】:

  • 在加载视图之前执行 var_dump($data) 会发生什么?
  • 你可以简单地改变这一行 -> $data = array('upload_data' => $this->upload->data());到 $data = $this->upload->data();然后访问名称 $data['file_name'];

标签: php codeigniter upload


【解决方案1】:

文件名可以是任何你想要的。这在文档中很明显,您可能一直在阅读错误的页面或其他内容:

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.

或者:

可以从 $this->upload->data() 中获取,它是一个数组。

$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"
)

【讨论】:

    【解决方案2】:

    使用以下代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 2016-02-10
      • 1970-01-01
      相关资源
      最近更新 更多