iOS上的PHP空TMP_NAME上传图片 [英] PHP empty TMP_NAME on iOS Upload Image
问题描述
我目前正在开发一个iOS项目,该项目要求用户将图像上传到服务器.
I am currently developing an iOS project that requests the user to upload an image to the server.
我目前在Objective-C的班级中有以下代码:
I currently have this code in my class in Objective-C:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:[SSKeychain passwordForService:@"ID" account:@"SpotterBike"] forKey:@"ID"];
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSString *FileParamConstant = @"uploadedfile";
NSData *imageData = UIImageJPEGRepresentation(image, 1);
if (imageData){
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request setURL:[NSURL URLWithString:@"http://212.92.57.155/App/UploadImage.php"]];
但是,尽管服务器接收到该图像,但是我无法对其进行处理以进一步使用它.如您所见,tmp_name
为空,所以我无法将图像移至目录.
However, although the server recieves the image, I am not able to process it for further usage in it. As you see, tmp_name
is empty, so I am not able to move the image to the directory.
print_r($_FILES['uploadedfile']);
(
[name] => image.jpg
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
为什么会这样?
推荐答案
只要有人遇到相同的问题,经过几天的研究,我就发现了问题.
Just in case someone is facing the same problem, after some days of research, I have found the problem.
这些图片是通过3G移动连接上传的,因此,在开始上传直到完成之前,beetwen有一定的延迟.
The images were uploaded with 3G mobile connections, as a result, there was certain amount of delay in beetwen the time the upload started until it finished.
由于MAX_REQUEST_TIMEOUT
属性的值较低,因此在超出MAX_REQUEST_TIMEOUT
之前图像无法到达服务器,因此上载被中止.
Due the fact that, the MAX_REQUEST_TIMEOUT
property had a low value, the image was not able to reach the server before the MAX_REQUEST_TIMEOUT
exceeded, so the upload was aborted.
希望有帮助!
这篇关于iOS上的PHP空TMP_NAME上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!