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?

图片转代码服务由CSDN问答提供 功能建议

我需要用户上传两张图片,然后将图片作为base64插入并发布到一个平静的地方 网络服务。

当我在服务器上使用文件时,我能够编码和发布图像并获得成功的响应,但我需要做的是使用最终用户动态上传的图像 。

这是我的表单:

 &lt; form action =“dcams.php”enctype =“multipart / form-data”方法 =“post”&gt; 
&lt; input name =“MAX_FILE_SIZE”type =“hidden”value =“30000”&gt; 发送此
文件:&lt; input name =“front”type =“file”&gt; 并且此文件:&lt; input name = 
“back”type =“file”&gt;  &lt; input type =“submit”value =“发送文件”&gt; 
&lt; / form&gt; 
   
 
 

这是我的PHP脚本,它与静态文件一起使用 在服务器上但我需要从发布的图像中获取文件内容:

 &lt;?php 
header('Content-type:application / json'); 
 \  n $ 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”=&gt;“”,
“pass”=&gt;“”,\  n“target”=&gt; array(
“license”=&gt; array(
“front”=&gt;“$ base64_front”,
“back”=&gt;“$ base64_back”,
“state”  =&gt;“CT”
),
“age”=&gt;“21 +”
),
“service”=&gt;“”
); 
 $ 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_RETURNTRANS  FER,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“$ 结果“; 
&GT?;  
   
 
 

我想做的是这样的事情:

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

但这不起作用。 有谁知道这样做的正确方法?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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);
    
    点赞 评论

相关推荐 更多相似问题