我需要auser上传两个图像,然后将图像作为base64编入并发布到一个安静的Web服务。
当我在服务器上使用文件时,我能够编码和发布图像并获得成功的响应,但我需要做的是使用最终用户动态上传的图像。
这是我的表格:
<form action="dcams.php" enctype="multipart/form-data" method="post">
<input name="MAX_FILE_SIZE" type="hidden" value="30000"> Send this
file: <input name="front" type="file"> And this file: <input name=
"back" type="file"> <input type="submit" value="Send Files">
</form>
这是我的PHP脚本,它与服务器上的静态文件一起使用,但我需要从发布的图像中获取文件内容:
<?php
header('Content-type: application/json');
$imagedata_front = file_get_contents("front.jpg");
$base64_front = base64_encode($imagedata_front);
$imagedata_back = file_get_contents("back.jpg");
$base64_back = base64_encode($imagedata_back);
$url = "https://xxxxxxxxxxxx.com";
$data = array(
"user" => "",
"pass" => "",
"target" => array(
"license" => array(
"front" => "$base64_front",
"back" => "$base64_back",
"state" => "CT"
),
"age" => "21+"
),
"service" => ""
);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
echo "$result";
?>
我想做的是这样的事情:
$imagedata_front = $_FILES['front'];
$base64_front = base64_encode($imagedata_front);
$imagedata_back = $_FILES['back';
$base64_back = base64_encode($imagedata_back);
但这不起作用。有谁知道这样做的正确方法?
答案 0 :(得分:1)
你接近原来的尝试。 $ _FILES数组是一个关联数组,其中一个entries(tmp_name)是已上载文件的服务器上的位置。
这是一个非常好的link到单个图像示例,包括错误检查,最大文件大小处理和文件类型检查。在访问之前,您需要移动上传的文件。
$imagedata_front = file_get_contents('/path/to/moved/file');
$base64_front = base64_encode($imagedata_front);
$imagedata_back = file_get_contents('/path/to/moved/file');
$base64_back = base64_encode($imagedata_back);