Home > Backend Development > PHP Tutorial > 使用ThinkPHP+Uploadify实现图片上传功能_PHP

使用ThinkPHP+Uploadify实现图片上传功能_PHP

WBOY
Release: 2016-06-01 11:51:22
Original
740 people have browsed it

ThinkPHP

首先,将下载的Uploadify压缩包解压放到公共文件夹内。实现代码如下:

前台html部分:

<script src="/uploadify/jquery.min.js" data-ke-src="/Public/uploadify/jquery.min.js" type="text/javascript">
<script src="/uploadify/jquery.uploadify.min.js" data-ke-src="/Public/uploadify/jquery.uploadify.min.js" type="text/javascript">
<link rel="stylesheet" type="text/css" href="/uploadify/uploadify.css" data-ke-src="/Public/uploadify/uploadify.css">
<script type="text/javascript">
var img_id_upload=new Array();//初始化数组,存储已经上传的图片名
var i=0;//初始化数组下标
$(function() {
 $('#file_upload').uploadify({
 'auto' : false,//关闭自动上传
 'removeTimeout' : 600,//文件队列上传完成1秒后删除
 'swf' : '/Public/uploadify/uploadify.swf',
 'uploader' : '/_URL_/Article/uploadify',
 'method' : 'post',  //方法,服务端可以用$_POST数组获取数据
 'buttonText' : '选择图片',//设置按钮文本
 'multi' : true,//允许同时上传多张图片
 'uploadLimit' : 8,//一次最多只允许上传10张图片
 'fileTypeDesc' : 'Image Files',//只允许上传图像
 'fileTypeExts' : '*.gif; *.jpg; *.png',//限制允许上传的图片后缀
 'fileSizeLimit' : '2000KB',//限制上传的图片大小
 'onUploadSuccess' : function(file, data, response) { //每次成功上传后执行的回调函数,从服务端返回数据到前端
$('#image').append('<div style="float:left;margin:2px 0 0 2px"><img     style="max-width:90%" height="100px" src="/uploads/'+data+'" data-ke-src="/uploads/'+data+'" height=80 width=80 / alt="使用ThinkPHP+Uploadify实现图片上传功能_PHP" >');
  img_id_upload[i]=data;
  i++;
 }
 });
});
</script>
<input type="file" name="file_upload" id="file_upload" />
<p><a href="javascript:$('#file_upload').uploadify('upload','');" data-ke-src="javascript:$('#file_upload').uploadify('upload','');">上传</a>
</p>
<br />
<div id="image" class="image"><br />
Copy after login

action执行代码部分:

public function uploadify()
 {
 $ph=M('Upload');
 import('ORG.Net.UploadFile');
 $upload = new UploadFile();// 实例化上传类
 $upload->maxSize = 93145728 ;// 设置附件上传大小
 $upload->saveRule =rand(1,9999);
 $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg','flv','avi','mov');// 设置附件上传类型
 $upload->savePath = './Uploads/';// 设置附件上传目录
  if(!$upload->upload())
  {// 上传错误提示错误信息
  $this->error($upload->getErrorMsg());
  }else{// 上传成功 获取上传文件信息
  $info = $upload->getUploadFileInfo();
  }
  for($i=0;$i<count($info);$i++)
  {
  $data['name']=$info[$i]['savename'];
  $data['size']=$info[$i]['size'];
  $data['type']=$info[$i]['extension'];
  echo $info[$i]['savename'];
  $rs=$ph->add($data); 
  }
  /*if($rs)
  {
  $this->success("成功");
  }else
  {
  $this->error("失败");
  }*/
 }
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template