使用php://input接收手機圖片上傳二進制流

1.客戶端模擬圖片上傳程序(test.php):

    <?php  
    $data=file_get_contents('1.png');  
    $http_entity_body = $data;  
    $http_entity_type = 'application/x-www-form-urlencoded';  
    $http_entity_length = strlen($http_entity_body);  
    $host = '127.0.0.1';  
    $port = 80;  
    $path = '/upload.php';  
    $fp = fsockopen($host, $port, $error_no, $error_desc, 30);  
    if ($fp) {  
      fputs($fp, "POST {$path} HTTP/1.1\r\n");  
      fputs($fp, "Host: {$host}\r\n");  
      fputs($fp, "Content-Type: {$http_entity_type}\r\n");  
      fputs($fp, "Content-Length: {$http_entity_length}\r\n");  
      fputs($fp, "Connection: close\r\n\r\n");  
      fputs($fp, $http_entity_body . "\r\n\r\n");  
       
      while (!feof($fp)) {  
        $d .= fgets($fp, 4096);  
      }  
      fclose($fp);  
      echo $d;  
    }  
    ?>  

 

2.服務端接收程序(upload.php):

    <?php  
    error_reporting(E_ALL);  
    function get_contents() {   
      $xmlstr = file_get_contents('php://input')?file_get_contents('php://input') : gzuncompress($GLOBALS['HTTP_RAW_POST_DATA']);//得到post過來的二進制原始數據  
      $filename=time().'.png';  
      if(file_put_contents($filename,$xmlstr)){  
     echo 'success';  
      }else{  
     echo 'failed';  
      }  
    }  
    get_contents();  
    ?>  
 執行test.php,看看你的根目錄是不是有新的圖片了!

 衆所周知,通過二進制方式的不能夠通過get,post方式拿到參數,那怎麼進行參數的傳遞呢?

答案就是讓客戶端把二進制進行一個分割組拼即可,服務端只需要把拿到的二進制字符串進行分隔就可以得到了。

 

3戶端模擬圖片上傳程序(test2php):

    <?php  
    $data=file_get_contents('1.png');  
    $data = 'www.4jcms.com[x]四季企業網站系統[]'.$data;//假設服務端需要額外的兩個參數,URL以及站名,我們用“[x]”進行組合,服務端也用這個進行拆分  
    $http_entity_body = $data;  
    $http_entity_type = 'application/x-www-form-urlencoded';  
    $http_entity_length = strlen($http_entity_body);  
    $host = '127.0.0.1';  
    $port = 80;  
    $path = '/upload2php';  
    $fp = fsockopen($host, $port, $error_no, $error_desc, 30);  
    if ($fp) {  
      fputs($fp, "POST {$path} HTTP/1.1\r\n");  
      fputs($fp, "Host: {$host}\r\n");  
      fputs($fp, "Content-Type: {$http_entity_type}\r\n");  
      fputs($fp, "Content-Length: {$http_entity_length}\r\n");  
      fputs($fp, "Connection: close\r\n\r\n");  
      fputs($fp, $http_entity_body . "\r\n\r\n");  
       
      while (!feof($fp)) {  
        $d .= fgets($fp, 4096);  
      }  
      fclose($fp);  
      echo $d;  
    }  
    ?>  


4服務端接收程序(upload2php):

    <?php  
    error_reporting(E_ALL);  
    function get_contents() {   
      $xmlstr = file_get_contents('php://input')?file_get_contents('php://input') : gzuncompress($GLOBALS['HTTP_RAW_POST_DATA']);//得到post過來的二進制原始數據  
      $arr = explode("[x]",$data,3);  
      $url = $arr[0];  //網址參數  
      $sitename = $arr[1];  //站名參數  
      $data = $arr[2];  //圖片二進制字符串  
      $filename=time().'.png';  
      if(file_put_contents($filename,$data)){  
     echo 'success';  
      }else{  
     echo 'failed';  
      }  
    }  
    get_contents();  
    ?>  

 

運行test2.php看看,是不是就得到你想要的結果了呢。

分享到:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章