php后台无法接受文件,ios afnetworking2.x上传图片 服务端php无法接受文件问题
//思路如下
1、將image轉(zhuǎn)成NSData
2、通過傳遞參數(shù)的形式 而不是formData的方式進(jìn)行傳輸,formData(有時可以接受到,有時候不行,不穩(wěn)定)
3、php獲取二進(jìn)制數(shù)據(jù) 進(jìn)行存儲。
//代碼如下
ios端
-(void)uploadImage:(UIImage?*)image{
AFHTTPRequestOperationManager?*manager?=?[AFHTTPRequestOperationManager?manager];
NSData?*?imageData=UIImageJPEGRepresentation(image,?0.1);//轉(zhuǎn)換成二進(jìn)制的數(shù)據(jù)流
NSDictionary?*parameters?=?@{@"file":?imageData};//這一步是重點
[manager?POST:@"http://www.d-shang.com/index.php?blog/upload"?parameters:parameters?constructingBodyWithBlock:^(id?formData)?{
}?success:^(AFHTTPRequestOperation?*operation,?id?responseObject)?{
NSLog(@"Success:?%@",?responseObject);
}?failure:^(AFHTTPRequestOperation?*operation,?NSError?*error)?{
NSLog(@"Error:?%@",?error);
}];
}
php端
public?function?upload(){
$data=$_POST['file'];
$filename=time().".jpg";
$file=ROOT."/log/".$filename;
$handle=fopen($file,"w+");
fwrite($handle,$data);
fclose($handle);
}
php端判斷二進(jìn)制流文件格式(不是必須的)
/**
*?通過二進(jìn)制流獲取文件類型
*?@param?string?$binary_data
*?@return?string
*/
function?get_file_type_binary_data($binary_data){
$str_info=?@unpack("c2chars",?substr($binary_data,?0,?2));
$type_code=?$str_info['chars1'].$str_info['chars2'];
switch?($type_code)?{
case?'7790':
$file_type?=?'exe';
break;
case?'7784':
$file_type?=?'midi';
break;
case?'8075':
$file_type?=?'zip';
break;
case?'8297':
$file_type?=?'rar';
break;
case?'255216':
case?'-1-40':
$file_type?=?'jpg';
break;
case?'7173':
$file_type?=?'gif';
break;
case?'6677':
$file_type?=?'bmp';
break;
case?'13780':
case?'-11980':
$file_type?=?'png';
break;
default:
$file_type?=?'unknown';
break;
}
return?$file_type;
}
//你可能還需要等比例壓縮圖片 ?在上傳前處理 不然數(shù)據(jù)流量太大了
-(UIImage?*)?imageCompressForWidth:(UIImage?*)sourceImage?targetWidth:(CGFloat)defineWidth{
UIImage?*newImage?=?nil;
CGSize?imageSize?=?sourceImage.size;
CGFloat?width?=?imageSize.width;
CGFloat?height?=?imageSize.height;
CGFloat?targetWidth?=?defineWidth;
CGFloat?targetHeight?=?height?/?(width?/?targetWidth);
CGSize?size?=?CGSizeMake(targetWidth,?targetHeight);
CGFloat?scaleFactor?=?0.0;
CGFloat?scaledWidth?=?targetWidth;
CGFloat?scaledHeight?=?targetHeight;
CGPoint?thumbnailPoint?=?CGPointMake(0.0,?0.0);
if(CGSizeEqualToSize(imageSize,?size)?==?NO){
CGFloat?widthFactor?=?targetWidth?/?width;
CGFloat?heightFactor?=?targetHeight?/?height;
if(widthFactor?>?heightFactor){
scaleFactor?=?widthFactor;
}
else{
scaleFactor?=?heightFactor;
}
scaledWidth?=?width?*?scaleFactor;
scaledHeight?=?height?*?scaleFactor;
if(widthFactor?>?heightFactor){
thumbnailPoint.y?=?(targetHeight?-?scaledHeight)?*?0.5;
}else?if(widthFactor?
thumbnailPoint.x?=?(targetWidth?-?scaledWidth)?*?0.5;
}
}
UIGraphicsBeginImageContext(size);
CGRect?thumbnailRect?=?CGRectZero;
thumbnailRect.origin?=?thumbnailPoint;
thumbnailRect.size.width?=?scaledWidth;
thumbnailRect.size.height?=?scaledHeight;
[sourceImage?drawInRect:thumbnailRect];
newImage?=?UIGraphicsGetImageFromCurrentImageContext();
if(newImage?==?nil){
NSLog(@"scale?image?fail");
}
UIGraphicsEndImageContext();
return?newImage;
}
參考資料:
[等比例縮小處理]http://www.cnblogs.com/yswdarren/p/3611934.html
總結(jié)
以上是生活随笔為你收集整理的php后台无法接受文件,ios afnetworking2.x上传图片 服务端php无法接受文件问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php增加数量,php – 更新列数量
- 下一篇: idea php 断点设置,php -