我有一个使用 codeigniter 上传文件的脚本,但它仍然使用提交按钮。我想上传而不点击提交。这是我的脚本:
控制器/gallery.php
<?php
class Gallery extends CI_Controller{
public function __construct()
{
parent::__construct();
}
function index(){
$this->load->model('MGallery');
if($this->input->post('upload')){
$this->MGallery->do_upload();
}
$this->load->view('gallery_view');
}
}
?>
模型/mgallery.php
<?php
class MGallery extends CI_Model{
var $gallery_path;
public function __construct()
{
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images');
}
function do_upload(){
$config = array(
'allowed_types'=>'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload',$config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image'=> $image_data['full_path'],
'new_image'=>$this->gallery_path . '/thumbs',
'maintain_ration'=>true,
'width'=>160,
'height'=>120
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
*views/gallery_view.php*
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<title>Gallery With CI</title>
<meta charset="UTF-8">
</head>
<body>
<div id="gallery">
</div>
<div id="upload">
<?php echo form_open_multipart('gallery'); ?>
<?php echo form_upload('userfile');?>
<?php echo form_submit('upload','Upload'); ?>
<?php echo form_close(); ?>
</div>
</body>
如果我想让它在没有提交按钮的情况下上传,我该怎么办?