作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我看过几个示例,但我认为我的问题可能出在 PHP 中。我正在尝试使用 AFNetworking 从 iphone 将图像上传到服务器。这是我的 obj-c 代码:
-(IBAction)uploadButtonClicked:(id)sender
{
NSData *imageToUpload = UIImageJPEGRepresentation(mainImageView.image, 90);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.THESERVER.com"]];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/PROJECT/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: imageToUpload name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: [%@]",response);
[MBProgressHUD hideHUDForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
if([operation.response statusCode] == 403){
NSLog(@"Upload Failed");
return;
}
NSLog(@"error: %@", [operation error]);
}];
[operation start];
}
这是我的 upload.php:
function upload(){
$uploaddir = '/uploads/';
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
sendResponse(200, 'Upload Successful');
return true;
}
sendResponse(403, 'Upload Failed');
return false;
}
当我尝试上传时失败了
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
并返回我设置的403/false状态码
最佳答案
这是一个愚蠢的错误...
我需要在 php 中
$uploaddir = 'uploads/';
代替
$uploaddir = '/uploads/';
关于php - AFNetworking 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10888167/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!