PHP JQ 异步上传并立即显示图片
2015-08-10 09:32
239 查看
<! DOCTYPE html> < html> < head> < meta charset ="GB2312" > < title> Insert title here </title > < script type ="text/javascript" src= "jquery.js"></ script > < script type ="text/javascript" > $(document).ready( function (){ $( "#i_1" ).load( function(){ var url = $("#i_1" ).contents().find( "#pic").html(); if (url != null){ $( "#tag_img" ).attr("src" ,url); } }); }); </ script> </ head> < body> < img id= "tag_img" src = "" /> < form enctype ="multipart/form-data" action= "upload_pic.php" method ="post" target= "upload_target"> <input type= "file" name ="img" class= "file" value ="" /> <input type= "submit" name ="uploadimg" value= "上传" /> </ form> < iframe id= "i_1" name = "upload_target"></ iframe > </ body> </ html>
重点:
1.form中的axtion="处理图片的有效PHP页面"
2.form中的target="iframe的name属性值"
3.JS中必须要有可以等待iframe加载完后处理iframe返回过来的图片地址。
处理图片的PHP程序页面:
<?php $tmp_name = $_FILES[ 'img'][ 'tmp_name']; $name = $_FILES[ 'img'][ 'name']; move_uploaded_file($tmp_name, './upload/'.$name); $img = './upload/'.$name; ?> <! DOCTYPE html> < html> < head> < meta name ="viewport" content= "initial-scale=1.0, user-scalable=no" > < meta http-equiv ="Content-type" content= "text/html;charset:utf-8" > < script type ="text/javascript" src= "jquery.js"></ script > </ head> < body> < div id= "pic" ><?php echo $img; ?></ div > </ body> </ html>
重点:
1.$_FILE全局超级变量可以接收到POST过来的文件,HTML input的name就是$_FILE['name']
2.接下来可以做很多处理,如判断是不是图片,图片大小....
3.move_uploaded_file($tmp,$location)函数把图片移动到相应的路径中去,$tmp指的是文件的临时
地址,$location指的是文件移动收的相对路径(包含文件名的路径!)
4.想办法在这个处理页面中找一个地方安放一下处理好的图片。< div id= "pic" ><?php echo $img; ?></ div >
来自:http://www.open-open.com/lib/view/open1410792776570.html
相关文章推荐
- 如何用PHPExcel读取超大excel文件
- PHP关闭Notice错误提示
- php提取字符串中的数字
- 高效PHP程序必知的53个技巧
- WIN2008+iis7.5+PHP+FASTCGI大一点的文件上传出现500错误的问题解决
- windows下matplotlib的安装
- thinkphp 更新数据时如果未修改数据,返回结果判断错误
- PHPCMS 目录结构
- shearphoto头像截取,切图头像上传,JS+PHP开发,发布了
- 解读PHP中的垃圾回收机制
- 6个超实用的PHP代码片段
- php生成二维码
- 深入浅出讲解:php的socket通信
- 使用pthreads实现真正的PHP多线程(需PHP5.3以上版本)
- php pthreads 多线程扩展的使用:一个较为稳定例子。
- php 分页
- php+ajax 城市联动
- H3C FTP协议
- PHP生成静态文件
- win2008 IIS与php整合