[英]HTML JPG Upload to PHP Script to POST RESTful Web Service
我需要一個用戶上傳兩個圖像,然后將這些圖像進行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);
但這是行不通的。 有人知道這樣做的正確方法嗎?
您與您最初的嘗試很接近。 $ _FILES數組是一個關聯數組, 條目 (tmp_name)之一是已上傳文件在服務器上的位置。
這是指向單個圖像示例的非常好的鏈接 ,其中包含錯誤檢查,最大文件大小處理和文件類型檢查。 您需要先移動上載的文件,然后才能訪問它們。
$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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.