Curl / PHP挑战传输图像和base64_encode / decode

时间:2018-02-12 12:27:24

标签: php curl

我正在将数据从一个linux-box传输到另一个linux-box。在generel中这很好用,但是我在传输图像方面遇到了麻烦。我测试过各种各样的东西。我希望有人可以帮助我。

    $filename  = "/home/user/image.jpg";
    $handle    = fopen($filename, "r");
    $data      = fread($handle, filesize($filename));
    $data      = base64_encode($data);

    # Transfer reading
    #$arrIn['changeCharset'] = "true";
    $arrIn['postFields'] = "action=test&data=$base64";
    $test = curlServerPost($arrIn);

这是我的CURL函数:

function curlServerPost($arrIn)
{
    $postFields = $arrIn['postFields'];
    $url = "$GLOBALS[remoteSite]"; // Where you want to post data

    $ch = curl_init(); // Initiate cURL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);  // Tell cURL you want to post something
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); // Define what you want to post
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the output in string format
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepter alle certifikater, se: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
    $res = curl_exec($ch); // Execute

    curl_close($ch); // Close cURL handle
    return($res);
}

最后,这是接收者(服务器)部分

$data = $_POST[data];
$data = base64_decode($data);

$stmt = $GLOBALS[pdo]->prepare("INSERT INTO cameraImages (cameraImagesId) VALUES ('')");
$stmt->execute();
$cameraImagesId = $GLOBALS[pdo]->lastInsertId();

$stmt = $GLOBALS[pdo]->prepare("UPDATE cameraImages SET cameraImagesFile='$data' WHERE cameraImagesId='$cameraImagesId'");
$stmt->execute();

最后一些评论: - 如果我没有收到服务器端的base_decode(但格式完全错误。图片中的错误)

  • 如果我在服务器端base_decode。没有收到任何东西。
  • 我想根本不进行编码/解码。如果我这样做。只存储了一小部分图像(损坏的图像)
  • 我的数据字段是LONGBLOB(mysql)
  • 服务器端的PHP是:PHP 5.3.3
  • 客户端的PHP是:PHP 7.0.27-0 + deb9u1

我尝试过各种各样的东西。遵循各种教程。它对我不起作用: - /

因此,如果有人能提出想法,我非常愿意测试并尝试:)

向前倾听,听取你的意见。

1 个答案:

答案 0 :(得分:0)

更迭。我只需要

urlencode($data);

并删除了base64_encode($ data); / base64_decode($ data); 我已经把图像放在数据库中了,因为许多人不推荐它。

相关问题