在我看来有以下代码:
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/