我正在创建一个程序,我想要上传一个文件来选择多个图像并将其保存到数据库中。我在编码方面不是那么天才,但我了解编程。
这是我到目前为止所做的,
Controller /upload.php
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->helper('date');
}
public function index() {
$this->load->view('v_page/header_view');
$this->load->view('v_document/upload_view', array('error' => ' ' ));
$this->load->view('v_page/footer_view');
}
public function do_upload() {
//print_r($_FILES);
$config = array(
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|png|jpeg',
//'overwrite' => TRUE,
'max_size' => '1000KB',
'max_width' => '1024',
'max_height' => '768',
//'encrypt_name' => true,
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('v_page/header_view');
$this->load->view('v_document/upload_error', $error);
$this->load->view('v_document/upload_view');
$this->load->view('v_page/footer_view');
}
else {
$upload_data = $this->upload->data();
$file_array = array(
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
'actions' => "modify",
);
$this->load->database();
$this->db->insert('tbl_image', $file_array);
$data = array('upload_data' => $this->upload->data());
$this->load->view('v_document/upload_success', $data);
}
}
public function upload_success() {
redirect('do_upload', 'refresh');
}
views/v_document/upload_view.php
<p class="alert alert-info">Upload New Document</p>
<?php echo form_open_multipart('upload/do_upload');?>
<label for="file">Select File To Upload:</label>
<input type="file" name="userfile" multiple="multiple" class="btn btn-file"/>
</br></br>
<input type="submit" value="Upload File" class="btn btn-primary"/>
<?php echo form_close(); ?>
views/v_document/upload_error.php
<div class="alert alert-error"><?php echo $error; ?></div>
views/v_document/upload_success.php
<?php $this->load->view('v_page/header_view'); ?>
<div class="alert alert-success">Your file was successfully uploaded!</div>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p>
<?php echo anchor(base_url().'public/img/uploads/' . $upload_data['file_name'], 'View uploaded image') ?>
</br>
<?php echo anchor('upload', 'Upload another image'); ?>
</p>
<?php $this->load->view('v_page/footer_view'); ?>
我的程序运行良好。我可以一张一张上传图片,但我想要的是一键上传多张图片。我需要它,因为我们需要在我们的网站上上传很多文档。
希望得到大家的帮助。
最佳答案
您需要做的是将文件标记的名称更改为数组,例如
<input type="file" name="userfile[]" multiple="multiple" class="btn btn-file"/>
然后需要将上传代码放入foreach中,如下所示
foreach($_FILES['userfile'] as $file)
{
if (!$this->upload->do_upload($file)) {
$error = array('error' => $this->upload->display_errors());
.
.
.
}
}
干杯。
关于php - 如何使用 Codeigniter 在单个文件上传中上传多个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25859584/