Iva*_*ova 4 php upload codeigniter file
我有一个几乎没有输入和文件输入的表单.我想检查文件输入是否为空.如果它是空的,请不要尝试上传,如果不是,则尝试上传.
我试过这样的事情:
$upld_file = $this->upload->data();
if(!empty($upld_file))
{
//Upload file
}
Run Code Online (Sandbox Code Playgroud)
你使用codeigniter的文件上传器类...并$this->upload->do_upload();
在条件语句中调用ahd检查它是否为true.
<?php
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);
}
Run Code Online (Sandbox Code Playgroud)
user_guide详细解释了这一点: http ://codeigniter.com/user_guide/libraries/file_uploading.html
但是,如果您在确定文件是否已被"上传"时已经死了,那么在您打电话给这个班级之前(不知道为什么会这样).您可以访问PHP $_FILES
超级全局..并使用条件来检查大小是否> 0.
http://www.php.net/manual/en/reserved.variables.files.php
更新2:这是实际工作代码,我自己使用CI 2.1在头像上传器上使用它
<?php
//Just in case you decide to use multiple file uploads for some reason..
//if not, take the code within the foreach statement
foreach($_FILES as $files => $filesValue){
if (!empty($filesValue['name'])){
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);
}
}//nothing chosen, dont run.
}//end foreach
Run Code Online (Sandbox Code Playgroud)