Я пытаюсь загрузить изображение на сервер из приложения iPhone.
Код PHP для загрузки изображения следующий:
if(isset($_POST['insertImage']))
{ //INSERT IMAGE -------------------
$method=safeData($_POST['insertImage']);
if($method=='true')
{
if(isset($_POST['userId']))
{
if(isset($_FILES['imageFile']))
{
if($_FILES['imageFile']['type']=="image/gif" || $_FILES['imageFile']['type']=="image/bmp" || $_FILES['imageFile']['type']=='image/jpeg' || $_FILES['imageFile']['type']=='image/jpg' || $_FILES['imageFile']['type']=='image/png')
{
if($_FILES['imageFile']['size']<=5250000)
{
$userId=safeData($_POST['userId']);
$newImgName=rand()."a".time().".".findexts($_FILES["imageFile"]["name"]); imgPath="./../admin/images/";
move_uploaded_file($_FILES['imageFile']['tmp_name'],$imgPath.$newImgName);
$data.=saveImageInfo($userId,$newImgName);
}
else
{
$data.="<List><ResponseCode>405</ResponseCode><Message>Maximum image size should not be more than 5mb </List>";
}
}
else
{
$data.="<List><ResponseCode>405</ResponseCode><Message>Invalid image format. only png,jpg,bmp formats supported</Message></List>"; }
}
else
{
$data.="<List><ResponseCode>405</ResponseCode><Message>imageFile method not found</Message></List>";
}
}
else
{
$data.="<List><ResponseCode>405</ResponseCode><Message>userId method not found</Message></List>";
}
}
else
{
$data.="<List><ResponseCode>405</ResponseCode><Message>invalid insertImage argument</Message></List>";
}
}
, и я использовал следующий код для загрузки изображения на сервер
+(NSData *)setUserImage:(NSData *)userImageData UserId:(int)UserId
{
NSString *result;
NSData *responseData;
@try {
NSURL *url = [[NSURL alloc] initWithString:webAddress];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"multipart/form-data; boundary=*****" forHTTPHeaderField:@"Content-Type"];//
NSMutableData *postBody = [NSMutableData data];
NSString *stringBoundary = [NSString stringWithString:@"*****"];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"insertImage\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"true"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userId\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%d",UserId] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imageFile\"; filename=\"myimagefile.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
//[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imageFile\"; filename=\"myimagefile.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[NSData dataWithData:userImageData]];// dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[req setHTTPBody: postBody];//putParams];
NSHTTPURLResponse* response = nil;
NSError* error = [[[NSError alloc] init] autorelease];
responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(isInDebugMode)
NSLog(@"Result: %@", result);
[url release];
[req release];
IceCreamManFinderAppDelegate *delegate1=(IceCreamManFinderAppDelegate *)[UIApplication sharedApplication].delegate;
if(error.domain!=nil)
{
NSString *errorDesc=[[error userInfo] objectForKey:@"NSLocalizedDescription"];
delegate1.globalErrorMessage=errorDesc;
return nil;
}
else
{
delegate1.globalErrorMessage=nil;
}
}
@catch (NSException* ex) {
NSLog(@"Error: %@",ex);
}
return responseData;
}
из кода выше я получаю следующий ответ от сервера
Неверный формат изображения.только форматы png, jpg, bmp
Я много пробовал, но не успешно.
Пожалуйста, подскажите, где я не прав?