2016-04-15 119 views
0

我有一個PHP文件採用兩個參數來通過一個移動應用程序(文本圖片),治療這種數據被用於以下命令:發送參數到另一個PHP文件與圖像

$image = file_get_contents("php://input"); 
$text = $_POST['Text']; 

下一步是將這些數據發送到通過POST方法另一個PHP文件(second.php),對於此我嘗試此代碼:

$params = array ('Text' => $text); 
$query = http_build_query ($params); 
$contextData = array ( 
       'method' => 'POST', 
       'header' => "Connection: close\r\n". 
          "Content-Length: ".strlen($query)."\r\n", 
       'content'=> $query); 

$context = stream_context_create (array ('http' => $contextData)); 
$result = file_get_contents (
        'second.php', // page url 
        false, 
        $context); 

然而我也需要發送圖像,我該怎麼做?

我需要發送的方式一個圖像參數中,我可以從這個命令選擇它 :$_FILES['imageUser'](它位於 second.php

+0

你有什麼想法:寫一個本地文件並將其名稱發送到同一臺計算機上的另一個程序?在IPC流中發送圖像數據?將文件寫入雲並傳遞其名稱? – wallyk

+0

所有'POST'數據都在'php:// input'中,所以它將同時包含圖像和$ _POST ['Text']'。所以'$ image = file_get_contents(「php:// input」)'不可能工作。 – Barmar

+0

我建議你學習如何使用'curl'做文件上傳。我不認爲你可以用'file_get_contents'來完成。 – Barmar

回答

0

您可以上傳文件保存到臨時位置,並將文件的位置+名稱發佈到second.php文件。

例如:

$target_dir = "uploads/"; 
// If you want unique name for each uploaded file, you can use date and time function and concatenate to the $target_file variable before the basename. 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
// Move the uploaded file 
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file) 
{ 
    // Now you can post the variable $image 
    $image = $target_file 
} 

後你second.php查詢,你甚至可以做unlink($image);刪除的文件,所以被移動的圖像不會吃你的服務器上的空間。

相關問題