上传的图像为空 - iOS AFNetworking PHP MySQL

时间:2015-12-24 17:15:32

标签: php ios mysql objective-c

我正在使用AFNetworking将图像上传到服务器。一切似乎都很好。但是,当我检查图像文件夹时,图像是0字节。 empty cannot be displayed.

我的PHP代码。

<?php
header('Content-type: application/json');


include 'connection.php';
$response = array();

if($_SERVER['REQUEST_METHOD']=='POST'){

$username = $_POST['username']
$image = $_POST['image'];

$sql = "SELECT id FROM photos ORDER BY id ASC";

        $res = mysqli_query($conn,$sql);

        $id = 0;

        while($row = mysqli_fetch_array($res)){
                $id = $row['id'];
        }


$path = "images/$id.jpg";

$actualpath = "http://www.example.com.au/ios/$path";

$sql = "INSERT INTO photos (image) VALUES ('$actualpath')";

 if(mysqli_query($conn,$sql)){
    file_put_contents($path,base64_decode($image));
    $response["Result"] = "TRUE";
    $response["message"] = "Successfully Uploaded";
 }


}else{
        $response["Result"] = "FALSE";
        $response["message"] = "Nothing";
    }

echo json_encode($response);
$conn->close();
?>

我正在使用以下Objective C代码。

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"username": _username.text,
                             };

        [manager POST:@"http://example.com.au/ios/addUser.php" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFormData:imageData name:@"image"];
            }
        success:^(AFHTTPRequestOperation *operation, id responseObject) {}

运行代码后,我从服务器获得成功响应,但图像为空。

1 个答案:

答案 0 :(得分:0)

我从教程Link获得了上述代码。并且不知道他们是如何做到的。经过互联网研究并完成下面的代码。它工作正常。

{{1}}