我有一个表单,只有几个输入和一个文件输入。我想检查文件输入是否为空。如果它是空的,不要尝试上传,如果不是,然后尝试上传。
我试过这样的东西:
$upld_file = $this->upload->data();
if(!empty($upld_file))
{
//Upload file
}
您使用codeigniter的文件上传器类。。。并在条件语句ahd中调用CCD_ 1以检查其是否为真。
<?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);
}
用户指南对此进行了详细解释: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
可能确实需要更多信息。但基本上,使用codeigniter上传类可以执行以下操作:
$result = $this->upload->do_upload();
if($result === FALSE)
{
// handle error
$message = $this->upload->display_errors();
}
else
{
// continue
}
代码点火器有很多功能,可能不需要在这里重新发明轮子。