我要尝试在PHP CodeIgniter中上传多个图像,但它显示的错误信息如下
错误消息:isset或空的非法偏移类型 文件名:libraries / Upload.php
行号:148
您没有选择要上传的文件。
我的观看文件
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php
if(isset($error))
{
echo $error;
}
?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<input type="file" name="userfile1" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
<?php
if(isset($upload_data))
{
echo "<ul>";
foreach ($upload_data as $item => $value):
echo"<li>";
echo $item . $value;
echo" </li>";
endforeach;
echo "</ul>";
}
?>
</body>
</html>
我的控制器文件
<?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'] = '10000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$fname= array('f1' => 'userfile','f2'=>'userfile' );
if ( ! $this->upload->do_upload($fname))
{
$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_form', $data);
}
}
}
?>
请帮我解决问题。
答案 0 :(得分:2)
因此,为了在同一个请求中进行多次上传,您可以更改CodeIgniter看到传入上传文件的方式,其方式类似于:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$errors = [];
$files = $_FILES;
$upload_count = count( $_FILES['userfile']['name'] );
for( $i = 0; $i < $upload_count; $i++ )
{
$_FILES['userfile'] = [
'name' => $files['userfile']['name'][$i],
'type' => $files['userfile']['type'][$i],
'tmp_name' => $files['userfile']['tmp_name'][$i],
'error' => $files['userfile']['error'][$i],
'size' => $files['userfile']['size'][$i]
];
$this->upload->initialize( $config );
// Use upload library for file validation
if( $this->upload->do_upload() )
{
// Upload was successful, do something ...
}
else
{
// Error: Upload Failed
$errors[] = $this->upload->display_errors('','');
}
}
这也意味着您将HTML中文件输入的名称更改为userfile []。