iOS: невозможно загрузить изображение через API успокоительного

У меня самое сложное время, когда я пытаюсь загрузить изображение с ios на веб-сервер через скрипт PHP. Я уже тестировал php-скрипт с помощью html-формы, и он работал отлично, но у меня много проблем при отправке с телефона.

В настоящее время я использую AFNetworking 2.0 и настраиваю его следующим образом:

 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://myexamplesite.com"]];
AFHTTPRequestOperation *op = [manager POST:@"mobile_app/upload.php" parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:_worldmoonproject.imageData name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];

А в NSLog только выкладывается следующее:

2015-08-18 17: 45: 37.378 WMP [3265: 280005] Успех: ***** (ноль)

Вот мой код сервера:

if(!empty($_POST))
{
//The directory to save the photos
define('MB', 1048576);
$errors = array();
$target_dir = "app_data/images/";
$file_name = md5(date('Y-m-d H:i:s:u'));
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$upload_valid = false;
$file_type = pathinfo($target_file, PATHINFO_EXTENSION);

//Verify the file is an image
$check = getimagesize($_FILES["file"]["tmp_name"]);

if ($check !== false)
{
$upload_valid = true;
}
else
{
$upload_valid = false;
$errors[] = "FILE_NOT_IMAGE";
}

//Check if the file already exists
if (file_exists($target_file))
{
$upload_valid = false;
$errors[] = "FILE_ALREADY_UPLOADED";
}

//Verify the file is not too large
if ($_FILES["file"]["size"] > 500*MB)
{
//the file is too large, display an error
$upload_valid = false;
$errors[] = "FILE_TOO_LARGE";
}

//Only allow image file types
if ($file_type != "jpg" && $file_type != "png" && $file_type != "jpeg")
{
$upload_valid = false;
$errors[] = "INVALID_FILE_TYPE";
}

//Verify the file is valid and upload it
if ($upload_valid)
{
//the file is valid
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir . $newfilename))
{
$msg = array('FILE_UPLOAD_SUCCESS' => "File uploaded successfully");
echo json_encode($msg);
}
else
{
$msg = array("FILE_UPLOAD_FAIL" => "Unable to upload file");
echo json_encode($msg);
}
}
else
{
//echo the error array in json
echo json_encode($errors);
}
}

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector