上传后尝试从codeigniter获取file_name

时间:2013-11-01 17:33:17

标签: php codeigniter upload

我想要做的是在上传文件后使用print_r将cvs文件解析到屏幕上,这样我就可以用它来做一些事情了。我似乎无法弄清楚如何使用$ data访问文件名。我尝试了几个东西,比如$ 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;
        }
    }
}
?>

2 个答案:

答案 0 :(得分:3)

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

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.

OR:

您可以从$ this-&gt; upload-&gt; 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"
)

答案 1 :(得分:1)

使用以下代码:

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