php - Codeigniter,测试文件 uploader 是否有文件

标签 php codeigniter

在我看来有以下代码:

if (isset($stockists)) {
    $id = $stockists->ID;
    echo form_open_multipart($system_settings['admin_folder'].'/stockists/form/'.$id);
} 
else {
    echo form_open_multipart($system_settings['admin_folder'].'/stockists/form/');
}

 <?php echo "<input type='file' name='userfile' size='20' />"; ?>

其中有许多其他文本输入字段会在提交时发送到数据库。文件 uploader 是我感兴趣的。

在我的 Controller 函数中,如何在提交后检查 uploader 中是否存在文件?

以下返回错误: $image = ($_FILES['userfile']);

如果 uploader 中存在文件,我需要检查条件语句。例如:

if ($_FILES['userfile']) {
  //do
}

但是这个方法不行。

最佳答案

super 全局 $_FILES

$_FILES['userfile'] 不是 bool 值。

if (strlen($_FILES['userfile']['tmp_name']) > 0) {
    // Yes, is uploaded
}

在数组中,您还有错误:

echo $_FILES['userfile']['error'];

CodeIgniter 方法

CodeIgniter 有一个上传类可以为您完成这项工作。

CodeIgniter's File Uploading Class permits files to be uploaded. You can set various preferences, restricting the type and size of the files.

下面是 CodeIgniter 文档中的示例:

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('upload_form', array('error' => ' ' ));
    }

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

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

有关完整示例,请参阅文档:CI File Upload Class

关于php - Codeigniter,测试文件 uploader 是否有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35058576/

相关文章:

php - 动态PHP网页保存为PDF

php - 如何刷新使用 phar.cache_list 指令缓存的 phar?

CodeIgniter Ion_Auth::忘记密码不起作用

mysql - 连接到外部 MySQL 数据库错误 mysqli_connect() : (HY000/2003)

php - 在 PHP 中从另一台服务器读取 xml 文件的方法有多少种?

php - 插入 base64 图像作为 wordpress 帖子附件

php - Laravel (HasMany) 不检索值

php - PHP 中 i18n 的最佳方式是什么

php - 想要针对特定​​列值输出列值

php - 如何防止数据库在缓存过期时被应用程序DDOS?