繁体   English   中英

检查文件是否要上传?笨

[英]Check if a file is going to be uploaded? CodeIgniter

我有一个几乎没有输入和文件输入的表单。 我想检查文件输入是否为空。 如果它是空的,请不要尝试上传,如果不是,则尝试上传。

我试过这样的事情:

$upld_file = $this->upload->data();
    if(!empty($upld_file)) 
    {
    //Upload file
    }

你使用codeigniter的文件上传器类...并调用$this->upload->do_upload(); 在条件语句中啊检查是否为真。

<?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);
}

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

可能确实需要更多信息。 但基本上,使用codeigniter上传类可以这样做:

$result = $this->upload->do_upload();

if($result === FALSE)
{

    // handle error
    $message = $this->upload->display_errors();
}
else
{
    // continue
}

codeigniter中有很多功能,可能不需要在这里重新发明轮子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM