1. 将图片转换为Base64编码,POST上传。PHP将Base64解码为二进制,再写出文件。缺点:不能上传较大的图片:
<?php
header('Content-type: text/json; charset=UTF-8');
$base64 = $_POST["file"]; // 得到参数
$img = base64_decode($base64); // 将格式为base64的字符串解码
$path = "md5(uniqid(rand()))".".jpg"; // 产生随机唯一的名字作为文件名
file_put_contents($path, $img); // 将图片保存到相应位置
?>
2. 通过IOS 提交NSData 数据,PHP接收并且转为二进制处理后生成图片
header("Content-Type: application/octet-stream");
$byte=$_POST['test'];
$byte = str_replace(' ','',$byte); //处理数据
$byte = str_ireplace("<",'',$byte);
$byte = str_ireplace(">",'',$byte);
$byte=pack("H*",$byte); //16进制转换成二进制
PublicClass::CacheData('','/upload/',$byte,'.jpg');//写入文件中!