吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5048|回复: 3
收起左侧

[其他转载] php+jquery利用FormData上传图片~麻麻再也不担心图片要单独传了~

[复制链接]
zhan170 发表于 2015-2-15 23:40
来源:傲雪星枫
QQ截图20150215233743.png
文件1:index.html
[HTML] 纯文本查看 复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title> FormData Demo </title>
  <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

  <script type="text/javascript">
  <!--
    function fsubmit(){
        var data = new FormData($('#form1')[0]);
        $.ajax({
            url: 'server.php',
            type: 'POST',
            data: data,
            dataType: 'JSON',
            cache: false,
            processData: false,
            contentType: false
        }).done(function(ret){
            if(ret['isSuccess']){
                var result = '';
				result += '运行时间=' + ret['yunxing'] + '<br>';
                result += 'name=' + ret['name'] + '<br>';
                result += 'gender=' + ret['gender'] + '<br>';
                result += '<img src="' + ret['photo']  + '" width="300">';
                $('#result').html(result);
            }else{
                alert('提交失敗');
            }
        });
        return false;
    }
  -->
  </script>

 </head>

 <body>
    <form name="form1" id="form1">
		<p>图片和数据~可以一起传了^_^o ~~~</p>
        <p>姓名:<input type="text" name="name" ></p>
        <p>性别:<input type="radio" name="gender" value="1">哥哥<input type="radio" name="gender" value="2">妹子</p>
        <p>头像:<input type="file" name="photo" id="photo"></p>
        <p><input type="button" name="b1" value="提交"></p>
    </form>
    <div id="result"></div>
 </body>
</html>

文件2:server.php
[PHP] 纯文本查看 复制代码
<?php
//程序运行时间
$starttime = explode(' ',microtime());
 //echo microtime();
$name = isset($_POST['name'])? $_POST['name'] : '';
$gender = isset($_POST['gender'])? $_POST['gender'] : '';

$filename = 'img/'.rand(1000,9999).time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));

$response = array();

if(move_uploaded_file($_FILES['photo']['tmp_name'], $filename)){
    $response['isSuccess'] = true;
    $response['name'] = $name;
    $response['gender'] = $gender;
    $response['photo'] = $filename;
}else{
    $response['isSuccess'] = false;
}
 //程序运行时间
 $endtime = explode(' ',microtime());
 $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
$response['yunxing']="本网页执行耗时:".$thistime." 秒。";
echo json_encode($response);

?>


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

冬丶雪帝 发表于 2015-2-15 23:49
不明觉厉
szhy007 发表于 2015-2-16 00:42
 楼主| zhan170 发表于 2015-2-16 23:20
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-22 17:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表