您现在的位置: 365建站网 > 365文章 > iOS上传图像到服务器PHP接收的方法

iOS上传图像到服务器PHP接收的方法

文章来源:365jz.com     点击数:538    更新时间:2018-06-02 00:40   参与评论
1. 将图片转换为Base64编码,POST上传。PHP将Base64解码为二进制,再写出文件。缺点:不能上传较大的图片

// iOS(Swift)
func upload(image: UIImage, url: String) {
    let imageData = UIImageJPEGRepresentation(image, 0.3) // 将图片转换成jpeg格式的NSData,压缩到0.3
    let imageStr = imageData?.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) // 将图片转换为base64字符串
    let params: NSDictionary = ["file": imageStr!]
    let manager = AFHTTPRequestOperationManager()

    // 采用POST的方式上传,因为POST对长度没有限制
    manager.POST(url, parameters: params, success: { (_: AFHTTPRequestOperation!, response: AnyObject!) in
        // 成功
    }) { (_: AFHTTPRequestOperation!, _: NSError!) in
        // 失败
    }
}

<?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.AFNetworking上传,PHP端通过正常接收网页上传方法来接收图片

static func uploadPortrait(image: UIImage, url: String) {
    let manager = AFHTTPRequestOperationManager()

    // fromData: AFN封装好的http header类,可以添加请求体
    manager.POST(url, parameters: [:], constructingBodyWithBlock: { (fromData: AFMultipartFormData!) in
        let pngData = UIImagePNGRepresentation(image)
        // name必须和后台PHP接收的参数名相同($_FILES["file"])
        // fileName为图片名
        fromData.appendPartWithFileData(pngData, name: "file", fileName: "image.png", mimeType: "image/png")     

        // let jpegData = UIImageJPEGRepresentation(image, 0.3)
        // fromData.appendPartWithFileData(jpegData, name: "file", fileName: "image.jpg", mimeType: "image/jpeg")
    }, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) in
        // 成功
    }) { (operation: AFHTTPRequestOperation!, error: NSError!) in
        // 失败
    }  
}

<?php
header('Content-type: text/json; charset=UTF-8' );

/**
* $_FILES 文件上传变量,是一个二维数组,第一维保存上传的文件的数组,第二维保存文件的属性,包括类型、大小等
* 要实现上传文件,必须修改权限为加入可写 chmod -R 777 目标目录
*/

// 文件类型限制
// "file"名字必须和iOS客户端上传的name一致
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
// && ($_FILES["file"]["size"] < 20000)) // 小于20k
{
    if ($_FILES["file"]["error"] > 0) {
        echo $_FILES["file"]["error"]; // 错误代码
    } else {          
        $fillname = $_FILES['file']['name']; // 得到文件全名
        $dotArray = explode('.', $fillname); // 以.分割字符串,得到数组
        $type = end($dotArray); // 得到最后一个元素:文件后缀

        $path = "../portrait/".md5(uniqid(rand())).'.'.$type; // 产生随机唯一的名字

        move_uploaded_file( // 从临时目录复制到目标目录
          $_FILES["file"]["tmp_name"], // 存储在服务器的文件的临时副本的名称
          $path);

        echo "成功";
    }
} else {
    echo "文件类型不正确";
}
?>


3.将图片封装在Http的请求报文中的请求体(body)中上传。也是AFN上传的原理

// 使用OC封装
#import <UIKit/UIKit.h>
@interface RequestPostUploadHelper : NSObject
+ (NSMutableURLRequest *)uploadImage:(NSString*)url uploadImage:(UIImage *)uploadImage params:(NSMutableDictionary *)params;
@end

#import "RequestPostUploadHelper.h"
@implementation RequestPostUploadHelper
+ (NSMutableURLRequest *)uploadImage:(NSString*)url uploadImage:(UIImage *)uploadImage params:(NSMutableDictionary *)params {
    [params setObject:uploadImage forKey:@"file"];

    //分界线的标识符
    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
    //根据url初始化request
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:10];
    //分界线 --AaB03x
    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
    //结束符 AaB03x--
    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
    //要上传的图片
    UIImage *image=[params objectForKey:@"file"];
    //得到图片的data
    NSData* data = UIImagePNGRepresentation(image);
    //http body的字符串
    NSMutableString *body=[[NSMutableString alloc]init];
    //参数的集合的所有key的集合
    NSArray *keys= [params allKeys];

    //遍历keys
    for(int i = 0; i < [keys count]; i++)
    {
        //得到当前key
        NSString *key = [keys objectAtIndex:i];
        //如果key不是file,说明value是字符类型,比如name:Boris
        if(![key isEqualToString:@"file"])
        {
            //添加分界线,换行
            [body appendFormat:@"%@\r\n",MPboundary];
            //添加字段名称,换2行
            [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
            //添加字段的值
            [body appendFormat:@"%@\r\n",[params objectForKey:key]];
        }
    }

    ////添加分界线,换行
    [body appendFormat:@"%@\r\n",MPboundary];
    //声明file字段,文件名为image.png
    [body appendFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"image.png\"\r\n"];
    //声明上传文件的格式
    [body appendFormat:@"Content-Type: image/png\r\n\r\n"];

    //声明结束符:--AaB03x--
    NSString *end=[[NSString alloc] initWithFormat:@"\r\n%@",endMPboundary];
    //声明myRequestData,用来放入http body
    NSMutableData *myRequestData = [NSMutableData data];
    //将body字符串转化为UTF8格式的二进制
    [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
    //将image的data加入
    [myRequestData appendData:data];
    //加入结束符--AaB03x--
    [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];

    //设置HTTPHeader中Content-Type的值
    NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
    //设置HTTPHeader
    [request setValue:content forHTTPHeaderField:@"Content-Type"];
    //设置Content-Length
    [request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
    //设置http body
    [request setHTTPBody:myRequestData];
    //http method
    [request setHTTPMethod:@"POST"];

    return request;
}
@end
// 使用
// Swift
static func uploadPortrait(image: UIImage, url:String) {
    // 使用
    let request = RequestPostUploadHelper.uploadImage(url, uploadImage: image, params: [:])
    // 异步网络请求
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
        if error != nil {
            // 失败
        } else {
            // 成功
        }
    }
}

<?php
// PHP代码和上一步相同
?>


4.iOS图片转换为NSData,通过POST上传。PHP接收POST参数,将NSData的16进制编码转换为PHP支持的二进制,再写出文件保存
暂时没有找到办法,PHP接收到16进制编码后, 使用算法 转换为二进制后无法输出图片

5.二进制POST上传。PHP直接将数据保存为图片
暂时没有找到办法,iOS端使用NSData的getBytes无法转换为二进制

iOS Post上传图片, 文件流的形式

最近看到群里很多小伙伴有问上传图片服务器收不到, 解析不了.  可以说就一个属性  ContentType , 不论你使用第三方的AFN, 还是自己封装NSURLSesstion. 都需要把ContentType 设置和后台的解析方式一样(可能术语不是很标准, 总之就这个意思哈,谅解)


下面贴一段AFN的上传图片代码吧, 亲测有效, 不过可能只是针对我的服务器.  只要和后台同学沟通好就行了

  //此body是向后台传的参数, 因为是上传图片, 所以只给个图片名就够了, 这个和后台去问

    NSDictionary * body = @{@"category":@"user",@"file":@"HeadImg"};

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    //ContentType设置

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",

                                                        

                                                         @"text/html",

                                                        

                                                         @"image/jpeg",

                                                        

                                                         @"image/png",

                                                        

                                                         @"application/octet-stream",

                                                        

                                                         @"text/json",

                                                        

                                                         nil];

   

    manager.requestSerializer= [AFHTTPRequestSerializer serializer];

   

    manager.responseSerializer= [AFHTTPResponseSerializer serializer];

   

    [manager POST:url parameters:body constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

//把image  转为data , POST上传只能传data

        NSData *data = UIImagePNGRepresentation(image);

        //上传的参数(上传图片,以文件流的格式)

        [formData appendPartWithFileData:data

  

                                    name:@"file"

        

                                fileName:@"gauge.png"

        

                                mimeType:@"image/png"];

       

    } progress:^(NSProgress * _Nonnull uploadProgress) {



    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

  //请求成功的block回调

        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

        NSLog(@"上传成功%@",dic);

            

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

          NSLog(@"上传失败%@",error);

    }];


PHP post 接口上传图片 [接收ios ||androip 两种接收图片数据流保存图片方式

// 保存会员头像
            if ($apiType != 'ios') {              

                $path = 'memberhead'; // 会员头像
                $postName = 'avatar';
                $data = array ();
                $model_MembersPhoto = array ();
                $date = date ( "Ym" );
                $webroot = YiiBase::getPathOfAlias ( 'webroot' ); // 返回程序入口文件所在目录
                $dirname = "/uploads/" . $path . "/" . $date . "/"; // 图片所在文件
                if (! $this->mkdirs ( $webroot . $dirname ))         // 创建图片所在目录
                {
                    $data ['status'] = "0"; // 创建图片保存目录失败
                } else {
                    $filepath = substr ( md5 ( md5 ( $_GET ["type"] . "_" . $memid . uniqid ( mt_rand (), true ) ) ), 2, 16 ); // 生成唯一值
                    $pic_file = CUploadedFile::getInstanceByName ( $postName );
                    if ($pic_file->size < 3 * 1024 * 1024 && $pic_file->size > 3 || 1==1) {
                        $ext = strtolower ( $pic_file->extensionName );
                        $newName = $filepath . '.' . $ext; // 生成文件名
                        if (in_array ( $ext, array ('jpg','gif','jpeg','png') )) {
                            if ($pic_file->saveAs ( $webroot . $dirname . $newName )) {
                                $data ['pstatus'] = '1'; // 图片文件保存成功                              
                                $modelMember ["Members"] ["avatar"] = $dirname . $newName;
                            } else {
                                $data ['pstatus'] = '2'; // 图片文件保存失败
                                $data ['name'] = 'error';
                            }
                        } else {
                            $data ['pstatus'] = '3'; // 图片文格式不对
                            $data ['ext'] = $ext;
                        }
                    } else {
                        $data ['pstatus'] = '4'; // 提示:文件大小不能超过3M 大于 3b
                        $data ['file'] = $pic_file;
                    }
                }
              
            } else {//IOS上传图片
                $date = date ( "Ym" );
                $webroot = YiiBase::getPathOfAlias ( 'webroot' ); // 返回程序入口文件所在目录
                $dirname = "/uploads/memberhead/" . $date . "/"; // 图片所在文件
                if (! $this->mkdirs ( $webroot . $dirname ))                 // 创建图片所在目录
                {
                    $data ['pstatus'] = "6"; // 创建图片保存目录失败
                }
                $filepath = substr ( md5 ( md5 ( $_GET ["type"] . "_" . $memid . uniqid ( mt_rand (), true ) ) ), 2, 16 );
                $newName = $filepath . '.png';
          
                $pic = base64_decode ( $_POST ['pic_2'] );  
                file_put_contents($webroot . $dirname . $newName, $pic);
                //file_put_contents($webroot . $dirname . $filepath . '.txt', $pic);
                $data ['pstatus'] = '1';
                $modelMember ["Members"] ["avatar"] = $dirname . $newName;              
              
            }     

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (538人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号