1

我要做的是在上传文件后使用 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;
        }
    }
}
?>
4

2 回答 2

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.

或者:

您可以从 $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"
)
于 2013-11-01T17:39:47.690 回答
1

使用以下代码:

if($this->upload->do_upload($file)){
 //Get uploaded file data here
 $allData=$this->upload->data();
 $myFile = $allData['file_name'];
}
于 2013-11-20T22:32:51.560 回答