您的位置:首页 > 编程语言 > PHP开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: