h5压缩图片上传 php_h5+php实现图片上传

html:

function uploadImage() {

//判断是否有选择上传文件

var imgPath = $("#uploadFile").val();

if (imgPath == "") {

alert("请选择上传图片!");

return;

}

//判断上传文件的后缀名

var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);

if (strExtension != 'jpg' && strExtension != 'gif'

&& strExtension != 'png' && strExtension != 'bmp') {

alert("请选择图片文件");

return;

};

var formdata = new FormData();

var fileObj = $("#uploadFile").get(0).files;

formdata.append("file", fileObj[0]);

formdata.append("aid", 30);

formdata.append("nickname", "你好");

$.ajax({

type: "POST",

url: 'test.php',

data: formdata,

processData: false,

contentType: false,

cache: false,

success: function(data) {

//alert("上传成功");

$("#imgDiv").empty();

$("#imgDiv").html(data);

$("#imgDiv").show();

},

error: function(XMLHttpRequest, textStatus, errorThrown) {

//alert("上传失败,请检查网络后重试");

console.log(XMLHttpRequest);

console.log(textStatus);

console.log(errorThrown);

}

});

}

test.php:

//如果接收的数据包含图片文件

if(isset($_FILES['file'])){

$aid = intval($_POST['aid']);

$nickname = ($_POST['nickname']);

//获取图片的临时路径

$image = $_FILES["file"]['tmp_name'];

//只读方式打开图片文件

$fp = fopen($image, "r");

//读取文件(可安全用于二进制文件)

$file = fread($fp, $_FILES["file"]["size"]); //二进制数据流

//保存地址

$imgDir = 'account_imgs/';

//要生成的图片名字

//$filename = date("Ym")."/".md5(time().mt_rand(10, 99)).".jpg"; //新图片名称

$filename = "2017new.jpg";

//新图片的路径

$newFilePath = $imgDir.$filename;

$data = $file;

$newFile = fopen($newFilePath,"w"); //打开文件准备写入

fwrite($newFile,$data); //写入二进制流到文件

fclose($newFile); //关闭文件

//写入数据库

$sql = "update account set nickname = '".$nickname."', img = '".$filename."' where aid = ".$aid;

if(!$db->query($sql)) {

Json_out(array('result'=>'imgfail'));

exit;

}else{

Json_out(array('result'=>'imgsuccess'));

exit;

}

} else{

$aid = intval($_REQUEST['aid']);

$nickname = $_REQUEST['nickname'];

$sql = "update `account` set nickname = '".$nickname."' where aid = ".$aid;

if(!$db->query($sql)) {

Json_out(array('result'=>'fail'));

exit;

}else{

Json_out(array('result'=>'noimgsuccess'));

exit;

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值