![](/img/trans.png)
[英]WordPress Events Calendar PHP not working on Godaddy server, but works in WAMP localhost
[英]PHP picture upload works on localhost but not on GoDaddy server
我们一直在为iOS和Android开发一个应用程序。 我们的视图之一使用POST将图片上传到服务器。 android版本可在我们的本地服务器和网站中使用。 但是,iOS版本目前仅在我们的本地服务器上运行。 这是用于编写POST请求的代码:
NSData * imageData = UIImageJPEGRepresentation(image,1.0);
// Create the request.
NSString* afterURL = [NSString stringWithFormat:@"upload.php?username=%@",username];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:
[NSString stringWithFormat:@"%@%@",url,afterURL]]];
[request setHTTPMethod:@"POST"];
//------------------------------------------------------
// used code from this tutorial: http://www.iriphon.com/2011/11/09/ios-uploading-an-image-from-your-iphone-to-a-server/
NSString *boundary = @"-----InstantLabor";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// This line of code came from http://stackoverflow.com/questions/20728317/upload-image-to-the-php-server-from-ios
// By creating the string this way instead of the other way, we were able to successfully upload the picture!
/*
[body appendData:[[NSString stringWithString:
[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", @"dl"]]
dataUsingEncoding:NSUTF8StringEncoding]];
*/
[body appendData:[@"Content-Disposition: form-data; name=\"attachment[userfile]\";filename=\"dl.jpg\"\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// adding the body we've created to the request
[request setHTTPBody:body];
//-----------------------------------------------------
// Create url connection and fire request
_parsedData = nil;
[NSURLConnection connectionWithRequest:request delegate:self];
当我们试图使其在本地服务器上运行时,上述代码在我们切换之前不起作用
[body appendData:[@"Content-Disposition: form-data; name=\"attachment[userfile]\";filename=\"dl.jpg\"\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
与
[body appendData:[[NSString stringWithString:
[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", @"dl"]]
dataUsingEncoding:NSUTF8StringEncoding]];
即使创建相同的字符串,一个也可以,而另一个则不行。 但是在连接到网站时它们都不起作用。 我们尝试过的一切最终都会吐出来
Undefined index: userfile
回到应用程序。 由于其中之一有效,我们知道这不是内存问题或特权问题。 我们不知道还有什么可能导致该问题。
在此先感谢您的帮助。
经过两天的调整,测试。 等我们没有运气解决问题。 相反,我们决定合并AFNetworking并将此Upload image从iOS移植到PHP以使图片上传生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.