检查文件是否将要上传? CodeIgniter [英] Check if a file is going to be uploaded? CodeIgniter

查看:139
本文介绍了检查文件是否将要上传? CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有少量输入和文件输入的表单。
我想检查文件输入是否为空。

I have a form with few inputs and a file input. I want to check whethere the file input is empty or not. If it is empty do not try to upload, if it is not then try to upload it.

我尝试过这样的:

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


推荐答案

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

you use codeigniter's file uploader class... and call $this->upload->do_upload(); in a conditional statement ahd check if its 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);
}

user_guide详细解释了这一点: http://codeigniter.com/user_guide/libraries/file_uploading.html

然而,
如果你是死的设置检查一个文件是否已经上传aka ..提交之前,你调用此类(不知道为什么你会)。您可以访问PHP $ _ FILES super global ..并使用条件检查大小是否> 0。

However, if you are dead set on checking whether a file has been "uploaded" aka.. submitted BEFORE you call this class (not sure why you would). You can access PHPs $_FILES super global.. and use a conditional to check if size is > 0.

http://www.php.net/manual/en/reserved.variables .files.php

更新2:这是实际的工作代码,我在使用CI 2.1的头像上传器

Update 2: This is actual working code, i use it on an avatar uploader myself using 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆