【发布时间】:2014-09-16 01:57:46
【问题描述】:
我正在创建一个程序,我希望在其中上传单个文件以选择多个图像并将其保存到数据库中。我在编码方面不是那么天才,但我懂编程。
这是我到目前为止所做的,
控制器/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'); ?>
我的程序运行良好。我可以一张一张上传图片,但我想要的是一键上传多张图片。我需要它,因为我们需要在我们的网站上上传大量文档。
希望得到大家的帮助。
【问题讨论】:
-
我正在学习node.js,我读到他们可以上传多个文件,所以试着学习一下。
标签: php codeigniter twitter-bootstrap file-upload