【问题标题】:Sending an image to aws via php通过php将图像发送到aws
【发布时间】:2019-01-13 08:23:58
【问题描述】:

我要做的主要问题是经过一些处理后将图像从另一台服务器发送到 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";
    }

我为 todo 尝试了一些东西,但它们没有用。你能帮我找到解决办法吗?谢谢。

【问题讨论】:

  • 您尝试过的“待办事项”是什么?我们不会为您编写代码。
  • 已编辑,base64_to_jpeg 正在工作,我在另一个 php 文件中使用它。

标签: php amazon-web-services curl libcurl


【解决方案1】:

您正在使用 if($_POST[]) 检查变量是否为真

你真正想要的是检查变量 isset

试试这个,看看有没有结果:

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

或者

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

卷曲本身应该可以工作

【讨论】:

  • 只检查 tmp 就足够了吗?
  • 不,这只是一个例子。你当然需要检查它们(如果它们都是你的代码正常工作所必需的)
  • 仍在等待回复,功能需要时间,抱歉耽搁了
  • 看起来还可以,目前没有问题。
猜你喜欢
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
相关资源
最近更新 更多