ThinkPHP Uploadify 图片上载
从官方网站下载的Uploadify最新版本:http://www.uploadify.com/download/
jQuery库是1.7.1版本。
下载好的Uploadify目录下面的文件有:
用到的文件有
uploadify.css
jquery.uploadify.min.js
下面先给出HTML代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
<link rel="stylesheet" type="text/css" href="/uploadify/uploadify.css" />
<script type="text/javascript" src="/uploadify/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/uploadify/jquery.uploadify.min.js"></script> <script type="text/javascript">
$(function(){
//这一行也是必要的,后面会调用
<?php $timestamp = time();?>
$('#editor_img').uploadify({
//这里往表单中添加数据
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>',
//这是上传到的文件夹
'folder' : '__ROOT__/Uploads'
},
'swf' : '/uploadify/uploadify.swf',
'uploader' : '/uploadify/uploadify.php',
'buttonClass' : 'upload-image',
'fileTypeExts' : '*.gif; *.jpg; *.png',
'width' : 28,
'height' : 28
});
});
</script>
</head>
<body>
<form id="form" action="{:U('Index/doData')}" method="post" class="think-form">
<!-- 注意这里的name必须写成 Filedata,下面会解释 -->
<input id="editor_img" type="file" name="Filedata" />
<input type="submit" value="提交" />
</form>
</body>
</html>
注意,uploadify.php函数是需要修改的,不然会报出很多错误,比如找不到文件,或者某个变量未定义等等,还可以修改返回值。
<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/ // Define a destination
$targetFolder = '/Uploads'; // Relative to the root $verifyToken = md5('unique_salt' . $_POST['timestamp']); if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
//这里接收传过来的目录
$targetFolder = $_POST['folder'];
//这里出现了Filedata,前面的文件名称对应这里就行,不然会报错
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; // Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
//修改返回值,本来默认是返回1,现在改为返回相对路径
echo $targetFolder. '/' . $_FILES['Filedata']['name'];
} else {
echo 'Invalid file type.';
}
}
?>
这样就可以把图片上传到服务器指定目录下了。
我不知道是版本问题还是别的原因,按照网上的做法总是出错,所以记下成功的配置,供需要的人参考。
接着更新一下,以上使用因为没有特别编辑Uploadify.php文件,用的是自带文件,所以前台的需要做相应的修改,比如后台用到了时间戳,前台需要传递参数等。不过关于Input的name在上面的例子里面为什么非要写成Filedata,还不是很清楚。
下面给出一个经典示例:
HTML页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Uploadify</title>
<script type="text/javascript" src="/uploadify/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/uploadify/swfobject.js"></script>
<script type="text/javascript" src="/uploadify/jquery.uploadify.js"></script>
<link href="/uploadify/uploadify.css" type="text/css" rel="stylesheet"/> <script type="text/javascript">
$(function(){
<?php $timestamp = time();?>
$('#file_upload').uploadify({
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>',
'folder' : '__ROOT__/Uploads'
},
'swf' : '/uploadify/uploadify.swf',
'uploader' : '/uploadify/uploadify.php',
'buttonClass' : 'upload-image',
'buttonText' :'选择文件',
'fileTypeExts' : '*.gif; *.jpg; *.png; *.pdf',
'width' : 500,
'height' : 50,
'auto' : false,
'cancelImg' :'/uploadify/uploadify-cancel.png',
'onUploadSuccess' : function(file, data, response) {
var html = '<p id="file_name">'+file.name;
html +='<a href="javascript:void(0)" onclick="delete_file_name(this);">删除</a>';
html +='<input type="hidden" name="file_name" value="'+data+'"/></p>';
$('#file_upload').after(html);
}
});
}); function delete_file_name(e){
var $this = $(e);
$this.parent('p').remove();
}
</script>
</head>
<body>
<div style="width:500px; margin:auto;">
<input id="file_upload" type="file" name="file_upload" />
<p id="file_name"></p>
<p>
<a href="javascript:$('#file_upload').uploadify('upload')">上传</a>|
<a href="javascript:$('#file_upload').uploadify('cancel')">取消上传</a>
</p>
</div>
</body>
</html>
Uploadify.php做了一些简单修改
<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/ // Define a destination
$targetFolder = '/Uploads'; // Relative to the root $verifyToken = md5('unique_salt' . $_POST['timestamp']); if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$targetFolder = $_POST['folder'];
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
//如果目录不存在则创建目录
if(!file_exists($targetPath))
{
mkdir($targetPath,0777);
}
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; // Validate the file type
$fileTypes = array('jpg','jpeg','gif','png','pdf','txt','mp3'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) {
//处理了中文命名文件的上传
move_uploaded_file($tempFile,iconv("UTF-8","gb2312",$targetFile));
//返回的值data
echo $targetFolder. '/' . $_FILES['Filedata']['name'];
} else {
echo 'Invalid file type.';
}
}
?>
效果如下图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAh4AAADYCAIAAAArj32dAAAOgklEQVR4nO3dz27cVnsH4LkLXYYA6x4iWwaCtI3atK477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cK/Mmzudz/rdJAowbpuOlvBX9ZV73d+sJTyT3us0LX1cDQM+NpGjpcvCRtMSSNNeG4iWpitgK6PlPj4vwEO30VlLbQWPPmvpeEFs5YmtPLd7+rwAD93moqV7oa9Yf9aSNYdH5cQiXhDr/XkBHrqP4g6xrHO0tIzWfqwu3VwKAz4Sg7hDrH1Tv2iplO8wWsLift/R0nsEgIclcbR0uVjU72n8cMD2KUs4wpoXxJoOcdfdAR6c9LOWlktVue6zlvYs6RIt7QPeaWv7acgVYMTSR8vKHXv/ecouay1JLojJFWDc+kdL7eWjpp/KjrWjNR1o/WhpyY/uy/4b+7wAD12C/1/LhqOl+6ShSyT0Po2O5wAwAv5XYABEtqELYj1+hnY+Q/u8AINl1gJAZKIFgMhECwCRiRYAIhMtAEQmWgCITLQAEJloASAy0QJAZKIFgMhECwCRiRYAIhMtAEQmWgCITLQAEJloASAy0QJAZKIFgMhECwCRiRYAIhMtAEQmWgCITLQAEJloASAy0QJAZKIFgMj+H9rHEdH5jhn5AAAAAElFTkSuQmCC" alt="" />
如图所示,同时添加多个文件,点击上传的时候先上传第一个,再点击一次然后上传第二个,如果需要一次性上传自己研究一下吧。
ThinkPHP Uploadify 图片上载的更多相关文章
- 使用ThinkPHP+Uploadify实现图片上传功能
首先,将下载的Uploadify压缩包解压放到公共文件夹内.实现代码如下: 前台html部分: <script src="/uploadify/jquery.min.js" ...
- Thinkphp框架图片上传实例
https://www.cnblogs.com/wupeiky/p/5802191.html [原文转载自:https://www.cnblogs.com/guoyachao/p/628286 ...
- uploadify图片上传发生Security Error
今天在使用uploadify进行图片上传的时候出现security error.其根本原因是flash跨域问题.主要原因是因为配了两个域名www.ttyouni.com 和 ttyouni.com 在 ...
- thinkphp给图片打水印不清晰
项目中打印条形码的函数,从thinkphp自带的water函数修改而来的. 贴上代码: /** * water2 * 改写thinkphp的water函数更强健的函数,增加了写入位置参数 去掉了alp ...
- ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据
前端HTML+JQuery 备注Jquery需要1.x版本,不能用2.x版本 1.引入必要文件及上传input <load file="__PUBLIC__/js/jquery-1. ...
- uploadify 图片上传
遇到的问题总结: 1.//图片排序 $("#pics").sortable(); 2.//上传的文件对象名,与后台所传参数名保持一致,最初因为这个名称错误浪费了许久时间 fileO ...
- uploadify图片上传配置
参考:http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html 官网地址:http://www.uploadify.com/ ...
- php thinkphp uploadify
模板文件: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w ...
- Java重点之小白解析--浅谈数据流形式图片上载
文档上载,上载也不知道哪个大神(混球)起的名字,读起来怪怪的,反正平时我只读上传. 闲话少说,直入主题.先等等这两天做文件上传,都快把宝宝折磨疯了,不会呀,各种查呀,最可悲的是废了老大功夫学会了传送文 ...
随机推荐
- Spring Aop重要概念介绍及应用实例结合分析
转自:http://bbs.csdn.net/topics/390811099 此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细 ...
- java小算法—大衍数列
题目: 中国古代文献中,曾记载过“大衍数列”, 主要用于解释中国传统文化中的太极衍生原理. 它的前几项是:0.2.4.8.12.18.24.32.40.50 ... 其规律是:对偶数项,是序号平 ...
- ABAP更改现有程序
语法: READ REPORT <prog> INTO <itab>. INSERT REPORT <prog> FROM <itab>. 假定下列简 ...
- 关于RGB转换YUV的探讨与实现
最近在Android手机上使用相机识别条形码工作取得了比较理想的进展,自动识别功能基本完成,然而在手动识别指定条形码图片时遇到困难,由于Zxing开源Jar包识别图片的颜色编码式为YUV,而普通的图片 ...
- div 水平居中
对需要水平居中的DIV层添加以下属性: margin-left: auto; margin-right: auto;
- Android开发具体解释之ListView具体解释一
列表ListView介绍和实例 1.ListView -- ListActivity -- ListAdapter 2.ArrayAdapter结合ListView进行显示 3.SimpleA ...
- 仿path菜单button的实现
path刚出来时.其菜单button也算是让大多数人感到了惊艳,如今看来事实上是非常easy的就是动画的结合. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- 关闭对话框,OnClose和OnCancel
我们知道,在对话框中,屏蔽ESC键自己主动退出能够选择重载OnCancel为哑函数的方法: void CXXXXDlg::OnCancel() { // TODO: Add ...
- mysql日期时间函数2
win7可以设定每周从哪一天开始,win2003等不能方便的修改.有的是周日开始,有的是周一开始.而工作中有的时候每周是从周六开始算的,有些数据需要按周统计,那么那种方式比较好呢? 通过下面的研究 ...
- 【转】正确使用Block避免Cycle Retain和Crash
原文地址:http://tanqisen.github.io/blog/2013/04/19/gcd-block-cycle-retain/ 使用指南:http://blog.csdn.net/nic ...