dongnanke4106 2016-02-20 14:45
浏览 54
已采纳

HTML JPG上传到PHP脚本以POST RESTful Web服务

I have a need for auser to upload two images and then for the images to be base64 enocded and posted to a restful webservice.

I am able to encode and post the images and get a successful response when i use a file on the server but what i need to do is use the image being uploaded by the end user dynamically.

Here is my form:

 <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>

Here is my PHP Script that works with a static file on server but I need to get the file contents from the posted images:

 <?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";
?> 

What I want to do is something like this:

$imagedata_front = $_FILES['front'];
$base64_front    = base64_encode($imagedata_front);

$imagedata_back = $_FILES['back';
$base64_back    = base64_encode($imagedata_back);

But this does not work. Does anyone know the proper way to do this?

  • 写回答

1条回答 默认 最新

  • doukong1391 2016-02-20 14:57
    关注

    you were close with your original attempt. the $_FILES array is an associative array and one of the entries (tmp_name) is the location on the server of the file that has been uploaded.

    Here is a very good link to a single image example with error checking, max file size handing and file type checking. You'll need to move the uploaded file before accessing them.

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵