[英]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.