1、下载uploadify,   我的是v3.2

2、模板页面引入:

<base href='{base_url()}' />
<script type="text/javascript" src="/public/admin/js/jquery.js"></script>
<script type="text/javascript" src="/public/js/ajaxfileupload.js"></script>
<script type="text/javascript" src="/public/js/uploadify/jquery.uploadify.min.js"></script>
<link rel="stylesheet" type="text/css" href="/public/js/uploadify/uploadify.css" />

3、模板页面使用:

<tr>
<td style="text-align:center;padding-top:10px;"><span class="red"> * </span>上传并扫描应用:</td>
<td>
<input id="uploadApk" name="uploaApk" type="file" />
<span id="upload_note">
{if $edit}<font color='red'>已上传应用“{$apply['name']}”&nbsp; </font>{else}<font color='red'>建议应用包100M以内</font>{/if}
</span>
</td> </tr>

4、js code:

<script>
window.apk_uploading_flag = 0;
$(function() {
var seid = '{$seid}' ;
var type = '' ;
var vid = "{$apply['vid']}";
$("#uploadApk").uploadify({
height : 30,
swf : '/public/js/uploadify/uploadify.swf',//Uploadify 自带的flash
uploader : '/admin/ajaxSelfUpload',//ajax提交页面
width : 120,
buttonText : '上传应用',
method : 'post',
debug : false,
fileTypeExts : '*.apk',
sizeLimit : 512000,
fileObjName : 'uploadApk',
progressData : 'speed' ,
formData : { 'session_tmp': '' },
onUploadStart:function(){
window.apk_uploading_flag = 1;
$("#uploadApk").uploadify('settings','formData',{ 'session': seid});
},
onUploadComplete:function(){
window.apk_uploading_flag = 0;
},
onUploadSuccess:function(file,data,response){
var data = JSON.parse(data)
if(data.status){
$("#upload_note font").html('应用上传并扫描成功');
}else{
$("#upload_note font").html(data.info);
}
}
}); </script>

5、controller中代码

1、seid取值

 $this->assign('seid', $this->input->cookie($this->config->item('cookie_prefix') . $this->config->item('sess_cookie_name')));

2、上传函数

        /**
* 上传应用本身并扫描
*/
public function ajaxSelfUpload() { $strError = '';
$this->load->library("MyUpload");//上传类
$tmpFile = $_FILES['uploadApk'];
$editId = $this->input->get('vid');
$apkSize = $tmpFile['size'];
$apkName = $tmpFile['name'];
//判断文件格式、大小、判断包名是否已经存在
//通过aapt获得apk的所有信息,将上传的apk解压到临时目录
$upload = new MyUpload($tmpFile);
$upload->setFileExt(array('apk'));
$upload->setMaxsize(1024 * 1024 * 500); //reset upload path with category
$uploadApkDir = $this->getSelfUploadDir();
$upload->setUploadPath($uploadApkDir);
if (!$upload->isAllowedTypes()) {
$strError = '上传文件不是有效的apk文件';
} elseif ($upload->isBigerThanMaxSize()) {
$strError = '上传文件最大不能超过 ' . intval($upload->getMaxsize() / 1024) . 'KB';
} //保证上传的生成的文件唯一而不覆盖其他文件
if (empty($strError) and $upload->upload(false, FALSE)) { $uploadApkFilePath = $upload->getUplodedFilePath();
$this->load->library('ParseApkInfo');
$ParseApkInfo = new ParseApkInfo($uploadApkFilePath);
$apkInfoArray = $ParseApkInfo->getApkMoreInfo(); if (!$ParseApkInfo->getErrorMessage() && $apkInfoArray) { $apkInfoArray['icon'] = $ParseApkInfo->createApkIcon('/auto/apply/img/', '/auto/apply/img/');
if (!$ParseApkInfo->getErrorMessage()) {
$apkInfoArray['size'] = $apkSize;
$apkInfoArray['size_mb'] = round($apkSize / 1024 / 1024, 2);
$apkInfoArray['apk'] = ToolsHelper::getFileAccessUrl($uploadApkFilePath);
$apkInfoArray['icon_url'] = ToolsHelper::getFileAccessUrl($apkInfoArray['icon']);
ajaxReturn("OK", true, $apkInfoArray);
}
}
ajaxReturn($ParseApkInfo->getErrorMessage(), false);
} else {
ajaxReturn($strError, false);
}
} /**
* 应用apk包存放目录
* @return string
*/
private function getSelfUploadDir() {
return ToolsHelper::getSelfUploadDir();
}

基本配置完成,但是由于是在管理后台上传文件,所以flash 没有上传session需要手工配置下CI(uploadify配置本身简单,就是再配置后台登录上传的session的时候我花费了很长 时间所以跟大家分享出来)

修改system\libraries下面的Session.php文件:

修改sess_read函数:修改了2处

 function sess_read()
{
// Fetch the cookie
//$session = $this->CI->input->cookie($this->sess_cookie_name);
//为了能够在各大浏览器支持falsh上传文件,对140行处进行以下修改 (修改第一处)
if($this->CI->input->post('session_tmp')){
$session = $this->CI->input->post('session_tmp');
}else{
$session = $this->CI->input->cookie($this->sess_cookie_name);
}
//修改结束 // No cookie? Goodbye cruel world!...
if ($session === FALSE)
{
log_message('debug', 'A session cookie was not found.');
return FALSE;
} // HMAC authentication
$len = strlen($session) - 40; if ($len <= 0)
{
log_message('error', 'Session: The session cookie was not signed.');
return FALSE;
} // Check cookie authentication
$hmac = substr($session, $len);
$session = substr($session, 0, $len); // Time-attack-safe comparison
$hmac_check = hash_hmac('sha1', $session, $this->encryption_key);
$diff = 0; for ($i = 0; $i < 40; $i++)
{
$xor = ord($hmac[$i]) ^ ord($hmac_check[$i]);
$diff |= $xor;
} if ($diff !== 0)
{
log_message('error', 'Session: HMAC mismatch. The session cookie data did not match what was expected.');
$this->sess_destroy();
return FALSE;
} // Decrypt the cookie data
if ($this->sess_encrypt_cookie == TRUE)
{
$session = $this->CI->encrypt->decode($session);
} // Unserialize the session array
$session = $this->_unserialize($session); // Is the session data we unserialized an array with the correct format?
if ( ! is_array($session) OR ! isset($session['session_id']) OR ! isset($session['ip_address']) OR ! isset($session['user_agent']) OR ! isset($session['last_activity']))
{
$this->sess_destroy();
return FALSE;
} // Is the session current?
if (($session['last_activity'] + $this->sess_expiration) < $this->now)
{
$this->sess_destroy();
return FALSE;
} // Does the IP Match?
if ($this->sess_match_ip == TRUE AND $session['ip_address'] != $this->CI->input->ip_address())
{
$this->sess_destroy();
return FALSE;
}
//为了能够在各大浏览器支持falsh上传文件,对199行处进行以下修改 (修改第二处)
if (stristr($this->CI->input->user_agent(),'shockwave'))
{
$this->sess_match_useragent = FALSE;
}
//修改结束 // Does the User Agent Match?
if ($this->sess_match_useragent == TRUE AND trim($session['user_agent']) != trim(substr($this->CI->input->user_agent(), 0, 120)))
{
$this->sess_destroy();
return FALSE;
} // Is there a corresponding session in the DB?
if ($this->sess_use_database === TRUE)
{
$this->CI->db->where('session_id', $session['session_id']); if ($this->sess_match_ip == TRUE)
{
$this->CI->db->where('ip_address', $session['ip_address']);
} if ($this->sess_match_useragent == TRUE)
{
$this->CI->db->where('user_agent', $session['user_agent']);
} $query = $this->CI->db->get($this->sess_table_name); // No result? Kill it!
if ($query->num_rows() == 0)
{
$this->sess_destroy();
return FALSE;
} // Is there custom data? If so, add it to the main session array
$row = $query->row();
if (isset($row->user_data) AND $row->user_data != '')
{
$custom_data = $this->_unserialize($row->user_data); if (is_array($custom_data))
{
foreach ($custom_data as $key => $val)
{
$session[$key] = $val;
}
}
}
} // Session is valid!
$this->userdata = $session;
unset($session); return TRUE;
}

疑问:刚开始配置完仍然上传不成功,后来排查原因,什么也没改动,然后不小心试了试就好了……所以如有问题@me

在此贴出上传类:MyUpload

application\libraries\MyUpload.php

<?php

class MyUpload
{ private $allowedTypes = array('image/jpg','image/bmp', 'image/jpe', 'image/jpeg', 'image/pjpeg', 'image/x-png','image/png','image/gif'); private $fileExt = array('jpg'); private $uploadPath = ''; private $maxSize = 307200;//300k public function __construct(array $file=array())
{
$this->file = $file;
} /**
* 设置文件的上传目录,绝对地址
* @param string $uploadPath
* @return null
*/
public function setUploadPath($uploadPath=UPLOAD_DIR)
{
$this->uploadPath = $uploadPath;
} /**
* 设置文件的大小,byte单位
* @param integer $maxSize
* @return null
*/
public function setMaxsize($maxSize)
{
$this->maxSize = $maxSize;
} /**
* 设置允许上传的文件mime
* @param array $allowedTypes 允许上传的文件类型数组 例如:array('image/jpg','image/bmp')
* @return null
*/
public function setAllowedTypes(array $allowedTypes)
{
$this->allowedTypes = $allowedTypes;
} /**
* 设置允许上传的文件的后缀名
* @param array $fileExt 例如:array('jpg','png')
* @return null
*/
public function setFileExt(array $fileExt)
{
$this->fileExt = $fileExt;
} /**
* 获取文件的大小
* @return integer
*/
public function getMaxsize()
{
return $this->maxSize;
} /**
* 获取允许上传的图片后缀名
* @param unknown_type $val
* @param array $array
* @return unknown_type
*/
public function getaAllowedExt()
{
return $this->allowedTypes;
} /**
* 检查文件大小,是否超过了文件的大小限制
* @return bool
*/
public function isBigerThanMaxSize()
{
return $this->file["size"] > $this->maxSize;
} /**
* 检查文件后缀名是否正确
* @return unknown_type
*/
public function isAllowedTypes()
{
// $fileExt = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
// $fileExt = array('jpg');
$ext = $this->getFileExt(); if (in_array($ext, $this->fileExt))
{
if ($this->file['tmp_name'] === FALSE)
{
return false;
} }
else
{
return false;
} return true;
} /**
* 验证文件的内容类型
* @return bool
*/
public function isAllowedMime()
{
if( in_array($this->file["type"],$this->allowedTypes) )
{
return true;
}
} /**
* 获取文件后缀名
* @return string|null
*/
public function getFileExt()
{
$x = explode('.', $this->file['name']);
return strtolower(end($x));
} /**
* 检查图片大小是否正确
* @param integer $width 被允许的宽度
* @param integer $height 被允许的高度
* @return bool
*/
public function isAllowedSize($width,$height)
{
//对于上传的文件类型,大小,尺寸等做验证。。此处暂时省略
$fileSize = getimagesize($this->file['tmp_name']);
$fileWidth = $fileSize[0];
$fileHeight = $fileSize[1];
if($fileWidth != $width or $fileHeight != $height)
{
return false;
}
return true;
}
/**
* 检查图片大小是否正确 只要不大于设置的最大宽高就可以
* @param integer $width 被允许的最大宽度
* @param integer $height 被允许的最大高度
* @return bool
*/
public function isAllowedMaxSize($width,$height)
{
$fileSize = getimagesize($this->file['tmp_name']);
$fileWidth = $fileSize[0];
$fileHeight = $fileSize[1];
if($fileWidth > $width or $fileHeight > $height)
{
return false;
}
return true;
} /**
* 上传图片
* @param bool $isReplace 如果存在同名图片是否覆盖
* @param bool $isNewName 是否产生新的文件名或是用上传文件自身名字
* @return bool
*/
public function upload($isReplace=true,$isNewName=true)
{
if($isNewName){
$filename = self::createKey().'.'.$this->getFileExt();
}else{
$filename = $this->file["name"];
} if (file_exists($this->uploadPath. "/" . $filename))
{
if($isReplace){
@unlink($this->uploadPath. "/" . $filename);
}else{
$tmpExt = '.'.$this->getFileExt();
$filename = rtrim($this->file['name'], $tmpExt);
$filename .= "_" . date('ymd',time()).'_'.date('His',time()).$tmpExt;
}
} self::createDir($this->uploadPath); if ( ! @copy($this->file["tmp_name"], $this->uploadPath. "/" . $filename))
{
if ( ! move_uploaded_file($this->file["tmp_name"], $this->uploadPath. "/" . $filename))
{ return false;
} }
$this->filePath = str_replace(UPLOAD_DIR,'',$this->uploadPath. "/" . $filename);
return true;
} /**
* 获取文件的真实名称
* @return string
*/
public function getRealName()
{
return $this->file['name'];
} /**
* 生成一串随机数字
* @return string
*/
public static function createKey()
{
$randpwd = '';
for ($i = 0; $i < 10; $i++)
{
$randpwd .= mt_rand(33, 500);
} return md5($randpwd);
} /**
* 返回被保存的带路径的文件名
* @return string
*/
public function getUplodedFilePath()
{
return @$this->filePath;
} /**
* 循环创建目录
* @param string $path
* @return null
*/
public static function createDir($path){
if(!is_readable($path)){
self::createDir( dirname($path) );
if(!is_file($path)) mkdir($path,0777);
}
} }

CI(2.2) 配置 jquery的上传插件Uploadify(v3.2) 上传文件的更多相关文章

  1. 强大的支持多文件上传的jQuery文件上传插件Uploadify

    支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...

  2. jQuery文件上传插件Uploadify(转)

    一款基于flash的文件上传,有进度条和支持大文件上传,且可以多文件上传队列. 这款在flash的基础上增加了html5的支持,所以在移动端也可以使用. 由于官方提供的版本是flash免费,html5 ...

  3. 【转】JQuery上传插件Uploadify使用详解及错误处理

    转自:http://www.jb51.net/article/43498.htm 关于JQuery上传插件Uploadify使用详解网上一大把,基本上内容都一样.我根据网上的步骤配置完成后,会报一些错 ...

  4. JQuery上传插件uploadify优化

    旧版的uploadify是基于flash上传的,但是总有那么些问题,让我们用的不是很舒服.今天主要分享下在项目中用uploadify遇到的一些问题,以及优化处理 官方下载 官方文档 官方演示 下面是官 ...

  5. jquery上传插件uploadify 报错http error 302 解决方法之一

    前段时间用到jquery上传插件uploadify时,始终出现系统报出 http error 302 的错误. 网上大量搜集信息,基本上都是说session值丢失的问题,根据网友提供的解决方案进行修改 ...

  6. JQuery上传插件Uploadify使用详解 asp.net版

    先来一个实例 Uploadify插件是JQuery的一个文件支持多文件上传的上传插件,ajax异步上传,实现的效果非常不错,带进度显示.不过官方提供的实例时php版本的,本文将详细介绍Uploadif ...

  7. js上传插件uploadify自动检测不到flash控件的问题

    [问题描述] 项目开发中,由于使用了js的一个上传插件uploadify,下载的是flash版本的,后来在谷歌浏览器上运行时经常报flash控件未安装,虽然下图是uploadify自动检测自动弹出来的 ...

  8. PHP 多图上传,图片批量上传插件,webuploader.js,百度文件上传插件

    PHP  多图上传,图片批量上传插件,webuploader.js,百度文件上传插件(案例教程) WebUploader作用:http://fex.baidu.com/webuploader/gett ...

  9. jQuery上传插件Uploadify使用帮助

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.它的功能特色总结如下: 支持单文件或多文件上传,可控制并发上传的文件数 在服务器端支持各种语言与之配合使用,诸如PHP, ...

随机推荐

  1. Node.js缓存

    Node.js Buffer(缓冲区) JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 ...

  2. 【转】解决IDEA新建项目名称为红色

    idea如果当前project用了版本控制器,其下面新建的所有的项目默认都是加入到版本控制里面,所以项目名称和文件都是红色的,如图: File-->Settings-->version c ...

  3. SpringBoot+thymelates入门

    在pom.xml当中加入这俩个依赖 <dependency> <groupId>org.springframework.boot</groupId> <art ...

  4. 修改request请求参数

    本质上来讲,request请求当中的参数是无法更改的,也不能添加或者删除: 但在后台程序中,一般对request的参数的操作,都是通过request的getParameter.getParameter ...

  5. 【5】Builder模式(构建者模式)

    一.引言 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成.例如一个采购系统中,如果需要采购员去采购一批电脑时,在这个实际需求中,电脑就是一个复杂的对象, ...

  6. java工程师_基础_阶段一_HTML笔记篇

    一.了解HTML语言 html:超文本标记语言. 二.HTML整体结构<html> <head> </head> <body> </body> ...

  7. drupal7 hook_validate

    原文:function hook_validate function hook_validate($node, $form, &$form_state) { if (isset($node-& ...

  8. CentOS7系列--1.3CentOS7用户管理

    CentOS7用户管理 1. 添加用户 [root@centos7 ~]# useradd jack [root@centos7 ~]# passwd jack Changing password f ...

  9. 谷歌新Logo如何做到只有305字节

    谷歌新旧Logo 谷歌换logo已经有一段时间了,对于更换Logo的问题,大家讨论的最多的是到底新老Logo哪个更好看. 但也有个别同学注意到了一个事实:谷歌的新Logo只有305字节那么大,而老的L ...

  10. 怎么配置eclipse的联想功能

    第一步:打开Eclipse,windows下,打开“window”→“Preferences” :mac下,打开“偏好设置”. 第二步:选择“Java”,展开,“Editor”,选择“Content ...