服务器基于PHP CodeIgniter,Android基于Volley实现多文件/图片上传(含服务器,web版和an

1,在controllers下的helpers新建文件upload_helper.php

<?php/** * Make multifile array input complaint with CI_Upload.<br> * For use files[ ] input name you must use it method. * * @author porquero * * @example * In Controller<br> * $this->load->helper(‘upload’);<br> * multifile_array();<br> * foreach ($_FILES as $file => $file_data) {<br> * $this->upload->do_upload($file); * … * * @link */function multifile_array(){if(count($_FILES) == 0)return;$files = array();$all_files = $_FILES[‘f_file’][‘name’];$i = 0;foreach ((array)$all_files as $filename) {$files[++$i][‘name’] = $filename;$files[$i][‘type’] = current($_FILES[‘f_file’][‘type’]);next($_FILES[‘f_file’][‘type’]);$files[$i][‘tmp_name’] = current($_FILES[‘f_file’][‘tmp_name’]);next($_FILES[‘f_file’][‘tmp_name’]);$files[$i][‘error’] = current($_FILES[‘f_file’][‘error’]);next($_FILES[‘f_file’][‘error’]);$files[$i][‘size’] = current($_FILES[‘f_file’][‘size’]);next($_FILES[‘f_file’][‘size’]);}$_FILES = $files;}说明:

a.注意里面的key为’f_file’,这就要求app或web在上传,建表单的时候将此值对应上。

b.该文件主要是遍历$_FILES,通过current得到当前file的信息转存到数组里,然后返回。注意转存后索引是从1开始的。转存的字段有name/type/tmp_name/error/size,使用next移动$_FILES[‘f_file’][‘key’]的指针。

2.views里新建upload_form.php,用来在web上模拟测试上传是否成功:

<html><head><title>Upload Form</title></head><body><?php echo $error;?><?php $data = array(‘type’=>’shop’, ‘id’=>’1’);?><?php echo form_open_multipart(‘upload/web_upload’, ”, $data);?><input type="file" name="f_file[]" multiple="multiple" size="20" /><br /><br /><input type="submit" value="upload" /></form></body></html>注意:

a,这里使用了CI框架的form_open_multipart新建一个multipart的表单,访问的是控制器upload里的web_upload方法,第三个参数$data,用于模拟向服务器传递的post请求参数。当然你也可以在下面加几个<input>.

b,input里name对应的是f_file[],这个是跟 服务器那边统一好的。

c,若要支持多文件上传加上multiple="multiple",不加的话一次只能上传一个文件。

3,views下新建upload_success.php,显示上传成功后的界面。

<html><head><title>Upload Form</title></head><body><h3>Your file was successfully uploaded!</h3><ul><?php foreach ($upload_data as $item => $value):?><li><?php echo $item;?>: <?php echo $value;?></li><?php endforeach; ?></ul><p><?php echo anchor(‘upload’, ‘Upload Another File!’); ?></p></body></html>注意:这里的$upload_data是控制器上传成功后传给view的数据。

4,接下来是最关键的一个类,在controllers文件夹下新建Upload.php,这是个控制器,上传最核心的。

<?phprequire_once APPPATH . ‘controllers/base/BASE_CI_Controller.php’;class Upload extends BASE_CI_Controller{private $m_type = ”;private $m_id = ”;private $m_path = ‘./application/cache/image’;private $m_error = array();public function __construct(){parent::__construct();$this->load->helper(array(‘form’, ‘url’, ‘upload’));//$this->load->model(‘picture_model’);}public function index(){$this->load->view(‘upload_form’, array(‘error’ => ‘ ‘ ));}public function app_upload(){$this->init_argc();multifile_array();foreach ($_FILES as $file => $file_data){$this->do_upload($file);}if($this->m_error == NULL || count($this->m_error) == 0){$this->output->set_output(json_encode(array(‘msg’=>’上传成功’)));}else{$this->output->set_output(json_encode($this->m_error));}}public function do_upload($file){$config[‘upload_path’]= $this->m_path;$config[‘allowed_types’] = ‘gif|jpg|png|jpeg’;$config[‘max_size’]= 10240;$config[‘max_width’]= 2000;$config[‘max_height’]= 2000;$config[‘file_name’] = Util::random_str();$this->load->library(‘upload’, $config);if ( ! $this->upload->do_upload($file)){$this->on_upload_error($this->upload->display_errors());}else{$upload_data = $this->upload->data();$this->on_upload_success($upload_data[‘file_name’]);}}public function do_upload2($file){$config[‘upload_path’]= $this->m_path;$config[‘allowed_types’] = ‘gif|jpg|png|jpeg’;$config[‘max_size’]= 10240;$config[‘max_width’]= 2000;$config[‘max_height’]= 2000;$config[‘file_name’] = Util::random_str();$this->load->library(‘upload’, $config);if ( ! $this->upload->do_upload($file)){$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_success’, $data);}}public function web_upload(){multifile_array();foreach ($_FILES as $file => $file_data){$this->do_upload2($file);}}private function init_argc() {$this->m_type = $this->getPost(‘type’);$this->m_id = $this->getPost(‘id’);$this->m_path = $this->getPath($this->m_type);}private function getPath($type){$path = ‘./application/cache/image/shop’;if($type == "shop"){$path = ‘./application/cache/image/shop’;}return $path;}private function on_upload_success($name){if($this->m_type == ‘shop’){//$this->picture_model->add_shop_picture($this->m_id, $this->m_type, $name);}else if($this->m_type == ‘avatar’){//$this->picture_model->add_user_avatar($this->m_id, $this->m_type, $name);}}private function on_upload_error($error){$this->m_error[‘msg’] = $error;}}?>解释如下:

a,这里Upload是继承的BASE_CI_Controller,也可以换成CI_Controller,在自己的Base_CI_Controller里封装了自己项目一些常用的安全校验逻辑;

每个人心中,都会有一个古镇情怀,流水江南,烟笼人家。

服务器基于PHP CodeIgniter,Android基于Volley实现多文件/图片上传(含服务器,web版和an

相关文章:

你感兴趣的文章:

标签云: