任何人都可以为我提供一个简单的codeigniter工作代码片段,用于一次上传2个图像(通过2个不同的输入字段)。我需要一次上传2张图片,或者一个接一个上传。并且两个图像都需要位于不同的位置。
我尝试通过两次调用上传功能来自己创建它,但它返回了最后一些图像,包括这些扩展:* .jpg.jpg。
任何人都可以提供帮助
答案 0 :(得分:0)
不幸的是,CodeIgniter上传类不支持多个文件。但是,您可以使用标准的PHP函数。
<form enctype="multipart/form-data" action="/upload/send" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="a_file" type="file" /><br />
Send another file: <input name="another_file" type="file" /><br />
<input type="submit" value="Send Files" />
</form>
然后在控制器中执行以下操作:
class Upload extends Controller {
function Upload()
{
parent::Controller();
}
function index()
{
$data = array();
$this->load->view('template/head');
$this->load->view('upload', $data);
$this->load->view('template/foot');
}
function send()
{
// TODO: error checking, and cleanse ['name'] to prevent hacks
// http://www.php.net/manual/en/features.file-upload.errors.php
move_uploaded_file(
$_FILES['a_file']['tmp_name'],
'/path/to/uploads/'.$_FILES['a_file']['name']
);
move_uploaded_file(
$_FILES['another_file']['tmp_name'],
'/path/to/uploads/'.$_FILES['another_file']['name']
);
}
}
http://www.php.net/manual/en/features.file-upload.post-method.php
答案 1 :(得分:0)
控制器
function create(){
// we are using TinyMCE in this page, so load it
$this->bep_assets->load_asset_group('TINYMCE');
if ($this->input->post('name')){
// fields are filled up so do the followings
$this->MProducts->insertProduct();
redirect('products/admin/index','refresh');
}else{
// this must be the first time, so set variables
$data['title'] = "Create Product";
$this->load->view('image_upload',$data);
}
}
模型
function insertProduct(){
$data = array(
'name' => db_clean($_POST['name']),
'shortdesc' => db_clean($_POST['shortdesc']),
'longdesc' => db_clean($_POST['longdesc'],5000),
...
...
'image1' => db_clean($_POST['image1']),
'image2' => db_clean($_POST['image2'])
);
$this->db->insert('omc_product', $data);
$new_product_id = $this->db->insert_id();
}
查看
echo form_open_multipart('products/admin/create')."\n";
echo "<p><label for='parent'>Category</label><br/>\n";
echo form_dropdown('category_id',$categories) ."</p>\n";
echo "<p><label for='pname'>Name</label><br/>";
$data = array('name'=>'name','id'=>'pname','size'=>25);
echo form_input($data) ."</p>\n";
...
echo "<p><label for='image1'>Select Image</label><br/>";
$data = array('name'=>'image1','id'=>'image1','size'=>80);
echo form_textarea($data) ."</p>\n";
echo "<p><label for='image2'>Select another image</label><br/>";
$data = array('name'=>'image2','id'=>'image2','size'=>80);
echo form_textarea($data) ."</p>\n";
...
...
echo form_submit('submit','create product');
echo form_close();