通过PHP将图像发送到AWS

时间:2018-08-06 11:26:56

标签: php amazon-web-services curl libcurl

我要做的主要问题是在经过一些过程之后将图像从另一台服务器发送到AWS。为此,我已经在第一台服务器中完成了数据库处理。它工作得很好,我测试了它。之后,我必须将此图像直接发送到AWS以启动另一个.php文件。我尝试通过cURL函数:

server1.php在第一台服务器中找到的php文件:

$url = 'http://myawsurl.com/server/test.php';
$imageData = file_get_contents($_FILES['fileToUpload']['tmp_name']);

$post_data = array(
    'tmp' => $_FILES["fileToUpload"]["tmp_name"],
    'type' => $_FILES["fileToUpload"]["type"],
    'name' => $_FILES["fileToUpload"]["name"],
    'data' => $imageData
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);

test.php在aws中找到的php文件:

  if ($_POST["tmp"] && $_POST["name"] && $_POST["type"] && $_POST["data"]) { 
        $target_dir_requests = "/requests/";
        create_folder_if_not_exist(ABSPATH . $target_dir_requests);
        $dir_for_req_server = ABSPATH . $target_dir_requests . basename($_FILES["fileToUpload"]["name"]);
        $dir_for_req_server = base64_to_jpeg($_POST["data"],$dir_for_req_server);
    // After some requests that i've to do and i've done 
    } else {
    echo "data sending error";
    }

我尝试做一些事情,但是他们没有用。您能帮我找到解决方案吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您正在使用if($ _ POST [])

检查变量是否为true。

您真正想要的是检查变量是否已设置

尝试一下,看看是否有任何结果:

if(isset($_POST['tmp'])){
   echo 'test';
}

$tmp = isset($_POST['tmp']) ? trim($_POST['tmp']) : null;
if(!empty($tmp)){
   echo 'test';
}

卷曲本身应该起作用