Я пытаюсь загрузить небольшой файл .png (50x50 пикселей) из моего приложения для iPhone на мой сервер Ubuntu.Я пробовал несколько подходов к проблеме, опубликованной здесь, в StackOverflow и в других местах в Интернете, но пока не увенчался успехом.Я разместил свой код Obj-C для загрузки файлов и регистрации ответов сервера, мой код PHP на сервере и ответ, который я получаю.Я был бы очень признателен за помощь в решении этой проблемы - пожалуйста, дайте мне знать, если вы обнаружите какие-либо проблемы с моим кодом - Спасибо!
Код Obj-C:
// This method uploads the image to the server
-(void)uploadImage{
NSURL *url = [NSURL URLWithString:@"http://mydomain.com/upload.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUseKeychainPersistence:YES];
NSString *fileName = [NSString stringWithFormat:@"%@",self.avatarFileName];
[request addPostValue:fileName forKey:@"name"];
// Upload an image
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:self.avatarFileName]);
// Make sure it's not null..
NSLog(@"%d",[imageData length]);
// Set the data and filename
[request setData:imageData withFileName:fileName andContentType:@"image/png" forKey:@"userfile"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(uploadRequestFinished:)];
[request setDidFailSelector:@selector(uploadRequestFailed:)];
[request startAsynchronous];
}
- (void)uploadRequestFinished:(ASIHTTPRequest *)request{
NSString *responseString = [request responseString];
NSLog(@"Upload response %@", responseString);
}
- (void)uploadRequestFailed:(ASIHTTPRequest *)request{
NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]);
}
Код PHP на сервере Ubuntu:
<?php
echo ' -- Hit Script --';
print_r($_FILES);
$target_path = "files/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There w
as an error uploading the file, please try again!"; }
?>
Ответ с сервера:
Upload response -- Hit Script --
Array
(
)
There was an error uploading the file, please try again!