0

使用 codeigniter 我已经上传了一些文件,但我无法读取这些文件。

    <?php 
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller {

    /**
     * file Info
     * @access priavte
     * @var object
     */
    private $uploadFileInfo;

    private $galary_path_url;

    /**
     * load the form and url helper library
     * load our new PHPExcel library
     * contuctor
     */
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));

        $this->load->library('excel');

        $this->galary_path_url = base_url() . 'upload/';
    }

    /**
     * load the upload form by defeault
     * @access public
     * @return void
     */
    function index()
    {
        $this->load->view('/upload/upload_form', array('error' => ' ' ));
    }

    /**
     * does the upload 
     * @access public
     * @return void
     */
    function do_upload()
    {
        //set up the config file 
        //only doc,docx,xls,xlsx files can be uploaded
    $config['upload_path'] = './upload/';
    $config['allowed_types'] = '|doc|docx|xls|xlsx';
    $config['max_size'] = '2000';

    //load the upload libraby with current config file
        $this->load->library('upload', $config);

    //if not uploaded
        if ( ! $this->upload->do_upload())
    {
            echo  $_FILES['userfile']['type'] ;
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('/upload/upload_form', $error);
        } 
        //uploaded successfully. 
        else
    {
            $this->uploadFileInfo = $data = array('upload_data' => $this->upload->data());
            $this->load->view('/upload/upload_success', $data);
            $this->pdfExport();
    }        
    }

    /**
     * detect the files and send to the right method
     * use the convert this to pdf file.
     * @access public
     * @return void Description
     */
    public  function pdfExport()
    {
        echo $filePath = $this->galary_path_url . $this->uploadFileInfo['upload_data']['file_name'];

            var_dump(file_exists($filePath));


    }

}
?>

但是 FILE_EXITS 显示错误。提前致谢

4

1 回答 1

0

要获取绝对文件名,您可能需要使用full_path而不是file_name. 这将为您提供文件的绝对文件系统路径,而无需自己构建路径。目前,您正在base_url与不会给您文件路径的文件名连接,它会给您一个URL.

代替;

echo $filePath = $this->galary_path_url . 
                 $this->uploadFileInfo['upload_data']['file_name'];
var_dump(file_exists($filePath));

和;

$fullPath = $this->uploadFileInfo['upload_data']['full_path'];
echo $fullPath;
var_dump(file_exists($fullPath));
于 2013-05-20T04:43:21.413 回答