Codeigniter上传文件名
问题描述:
通常情况下,我们可以使用$this->input->get('field_name')
或$this->input->post('field_name')
来获取表格数据Codeigniter
,这很好。Codeigniter上传文件名
在原始PHP
中,我们使用$_FILES["fileToUpload"]["name"]
来获取用户尝试上传的文件名。
我的问题是:有没有Codeigniter
的方法来获取需要上传的文件的名称?
我想说,我需要获取用户试图上传的文件名,然后尝试使用Codeigniter
库而不是使用原始PHP
全局$_FILES
变量将其保存在我的服务器中。
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' '));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
// get the user submitted file name here
if (! $this->upload->do_upload('userfile'))
{
$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);
}
}
}
?>
答
,如果你想在后台文件名:
$this->upload->file_name
它的工作基础上system/library/upload.php
此功能。
public function data()
{
return array (
'file_name' => $this->file_name,
'file_type' => $this->file_type,
...
);
}
如果需要获取文件名...
之前保存到服务器...你有工作在JavaScript
<?php echo "<input type='file' name='userfile' size='20' onchange='changeEventHandler(event);' />"; ?>
onchange事件在javascript:
<script>
function changeEventHandler(event){
alert(event.target.value);
}
</script>
在调用'$ this-> upload-> do_upload('userfile')'之前,我可以调用'$ this-> upload-> data()'吗? –
我编辑了我的答案@MahbubulIslam ...您是否期望在提交按钮之前获取文件名?或提交表格后 – Nawin
我想说我需要知道用户试图上传之前试图上传它在我的服务器上的文件名。 –