使用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显示为FALSE。提前谢谢
答案 0 :(得分:0)
获取绝对文件名you may want to use full_path
instead of 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));