mysql插入查询执行2次后添加s3 put object aws upload image

时间:2018-04-03 03:10:05

标签: mysql amazon-s3 sql-insert

当我想使用putObject将图像上传到aws时,我遇到了问题。图像上传成功,但问题是我的mysql插入查询执行了2次,所以我得到重复的数据。不知道为什么。但如果我删除putObject代码,插入查询工作正常(只执行1次)

这是我的PHP代码

<?php

use Aws\S3\S3Client;
include_once "koneksi.php";

require '../awss3/vendor/autoload.php';

$config = require('../awss3/app/config.php');

$s3 = S3Client::factory([
  'key' => $config['s3']['key'],
  'secret' => $config['s3']['secret']
  ]);

class emp{}


$image = $_POST['image'];

$name = $_POST['filename'];

$actualpath = $_POST['path'];

$uploader = $_POST['uploader'];

$receiver = $_POST['receiver'];

    $path = "../solusight_ss/".$name."";
    $save1 = "".$uploader."/".$name."";
    $save2 = "".$receiver."/".$name."";

    file_put_contents($path,base64_decode($image));

    $s3->putObject([
                'Bucket' => $config['s3']['bucket'],
                'Key' => "{$uploader}/{$name}",
                'Body' => fopen($path, 'rb'),
                'ACL' => 'public-read'
            ]);

    $s3->putObject([
                'Bucket' => $config['s3']['bucket'],
                'Key' => "{$receiver}/{$name}",
                'Body' => fopen($path, 'rb'),
                'ACL' => 'public-read'
            ]);

            //Remove the file
            unlink($path);


    $query = mysqli_query($con, "INSERT INTO tbimage (filename, path, uploader,receiver, status,category,folder)
                    VALUES ('$name', '$save1','$uploader','$receiver', 0,'Photo',0)");
    $query2 = mysqli_query($con, "INSERT INTO tbimage (filename, path, uploader,receiver, status,category,folder)
                    VALUES ('$name', '$save2','$uploader','$receiver', 0,'Photo',0)");

    if ($query && $query2){

        $response = new emp();

        $response->success = 1;

        $response->message = "Sending Picture..";

        die(json_encode($response));

    } else{

        $response = new emp();

        $response->success = 0;

        $response->message = "Error Upload image";

        die(json_encode($response));

    }


mysqli_close($con);

&GT;

注意:这是我的Android应用程序的服务器端代码

0 个答案:

没有答案