OC上传图片
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求
manager.responseSerializer = [AFHTTPResponseSerializer serializer];//响应
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"autosendtmail@gmail.com" forKey:@"DS_USER_NAME"];
[manager POST:@"http://.../DS_UserIconUpload.php" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"123.jpg"], 1);
[formData appendPartWithFileData:data name:@"DS_USER_PORTRAIT_IMG"
fileName:@"DS_USER_PORTRAIT_IMG_TMP" mimeType:@"image/jpg"];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",dic);
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
PHP接受图片并保存
$filePath = dirname(__FILE__)."/userIcon/".$DS_USER_NAME;
if (!file_exists($filePath)) {
mkdir($filePath,0777);
}
$date = date("Ymdhis");
move_uploaded_file($_FILES["DS_USER_PORTRAIT_IMG"]["tmp_name"], $filePath."/$date.jpg");
move_uploaded_file用来将临时文件移动到我们真正的路径下