php – AFNetworking上传图像

前端之家收集整理的这篇文章主要介绍了php – AFNetworking上传图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我看了几个例子,但我认为我的问题可能在 PHP中.我正在尝试使用AFNetworking从iPhone上传图像到服务器.这是我的对象代码
  1. -(IBAction)uploadButtonClicked:(id)sender
  2. {
  3.  
  4. NSData *imageToUpload = UIImageJPEGRepresentation(mainImageView.image,90);
  5. AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.THESERVER.com"]];
  6.  
  7. NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/PROJECT/upload.PHP" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
  8. [formData appendPartWithFileData: imageToUpload name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
  9. }];
  10.  
  11. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  12.  
  13. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responSEObject) {
  14. NSString *response = [operation responseString];
  15. NSLog(@"response: [%@]",response);
  16. [MBProgressHUD hideHUDForView:self.view animated:YES];
  17. } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
  18. [MBProgressHUD hideHUDForView:self.view animated:YES];
  19. if([operation.response statusCode] == 403){
  20. NSLog(@"Upload Failed");
  21. return;
  22. }
  23. NSLog(@"error: %@",[operation error]);
  24.  
  25. }];
  26.  
  27. [operation start];
  28. }

这是我的upload.PHP

  1. function upload(){
  2. $uploaddir = '/uploads/';
  3. $file = basename($_FILES['file']['name']);
  4. $uploadfile = $uploaddir . $file;
  5.  
  6. if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)) {
  7. sendResponse(200,'Upload Successful');
  8. return true;
  9. }
  10. sendResponse(403,'Upload Failed');
  11. return false;
  12.  
  13. }

当我尝试上传它失败

  1. if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)) {

并返回我设置的403 / false状态代码

这是一个愚蠢的错误

PHP我需要

  1. $uploaddir = 'uploads/';

代替

  1. $uploaddir = '/uploads/';

猜你在找的PHP相关文章