【发布时间】:2013-10-16 17:15:15
【问题描述】:
这是我的苗条框架 php 代码
$app->post('/uploadPicture', function () {
if (!empty($_FILES)) {
global $config;
$picturePath = $config->get('db', 'picture');
$allowedExts = array("jpg", "jpeg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" && $_FILES["file"]["size"] < 2500000 && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
} else {
if (move_uploaded_file($_FILES['file']['tmp_name'], $picturePath . $_FILES["file"]["name"])) {
echo "success";
}
}
} else {
echo "Invalid file type";
}
} else {
echo "no file to upload";
}
});
这是我在 iphone 端使用的。
-(void)uploadImage:(UIImage *)image withPersistentID:(NSString *)persistentID {
[[MSMAMobileAPIClient sharedClient] POST:@"uploadPicture" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 90);
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", persistentID];
[formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpg"];
} success:^(NSURLSessionDataTask * task, id responderData) {
self.itemImageBlock(YES);
} failure:^(NSURLSessionDataTask * task, NSError * error) {
NSLog(@"%@",[error.userInfo objectForKey:@"JSONResponseSerializerWithDataKey"]);
self.itemImageBlock(NO);
}];
}
在 iphone 端,它似乎就像服务器很忙一样挂起,然后它最终因超时而失败。
我一直在使用 CocoaRestClient 并且能够上传图片。
在尝试从 iphone 上传时,我还看到文件被添加到 php 的临时目录中。
我做错了吗?
编辑:我刚刚在uploadPicture 函数中添加了一个错误日志行,它甚至看起来都没有被iphone 调用! :(
EDIT2:这是返回错误消息的 NSLog
Error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x17d6e1c0 {NSErrorFailingURLStringKey=https://192.168.1.15/mamobile/index.php/uploadPicture, NSErrorFailingURLKey=https://192.168.1.15/mamobile/index.php/uploadPicture, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x17db1460 "The request timed out."}
EDIT3:我去掉了slim php框架,只是创建了一个只上传图片的简单php脚本。
<?php
if (!empty($_FILES)) {
//load configuration file
require_once 'Lite/Lite.php';
$config = new Config_Lite('config.ini');
$picturePath = $config->get('db', 'picture');
$allowedExts = array("jpg", "jpeg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" && $_FILES["file"]["size"] < 2500000 && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
} else {
if (move_uploaded_file($_FILES['file']['tmp_name'], $picturePath . $_FILES["file"]["name"])) {
echo "success";
}
}
} else {
echo "Invalid file type";
}
} else {
echo "no file to upload";
}
?>
然后我将代码更改为以这种方式工作。
NSString *urlString = [NSString stringWithFormat:@"https://%@/mamobile/uploadPicture.php", [[NSUserDefaults standardUserDefaults] stringForKey:@"serviceIPAddress"]];
AFHTTPSessionManager *imageSession = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:urlString]];
imageSession.responseSerializer = [MSJSONResponseSerializerWithData serializer];
[imageSession.requestSerializer setAuthorizationHeaderFieldWithUsername:@"fake username" password:@"fake password"];
[imageSession.securityPolicy setAllowInvalidCertificates:YES];
[imageSession POST:@"" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 90);
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", persistentID];
NSLog(@"uploading image '%@' with size = %@",fileName,[NSByteCountFormatter stringFromByteCount:imageData.length countStyle:NSByteCountFormatterCountStyleFile]);
[formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpg"];
} success:^(NSURLSessionDataTask * task, id responderData) {
NSLog(@"Success: %@", responderData);
self.itemImageBlock(YES);
} failure:^(NSURLSessionDataTask * task, NSError * error) {
NSLog(@"Error: %@",error);
self.itemImageBlock(NO);
}];
但我仍然收到相同的超时消息。所以和slim php框架无关。
【问题讨论】:
-
是成功块、失败块还是都不调用?如果调用其中一个块,请在其中设置断点并在调试器中键入
po task和po responderData或po error。如果没有调用任何块,则打开 AFURLSessionManager.m 并在dataTaskWithRequest: completionHandler:和AFURLSessionManagerTaskDelegate实现中的URLSession: task: didCompleteWithError:版本中放置断点。逐步检查这些方法并确保行为符合您的预期。 -
失败块被调用,它只是说它超时。
-
你能发布实际的错误吗?
-
@AaronBrager 我添加了错误消息。
标签: php ios objective-c slim afnetworking-2