我做了一個REST服務,它主要基於this tutorial。我還製作了一個基於this tutorial的REST請求庫。 (基本上,switch
上的一堆$_SERVER['REQUEST_METHOD']
)。 api也用cURL提出請求。
protected function executePost ($ch)
{
if (!is_string($this->requestBody))
{
$this->buildPostBody();
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
curl_setopt($ch, CURLOPT_POST, 1);
$this->doExecute($ch);
}
protected function doExecute (&$curlHandle)
{
$this->setCurlOpts($curlHandle);
$this->responseBody = curl_exec($curlHandle);
$this->responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
}
我有2個簡單的HTML表單,一個用get方法,另一個用於post方法。當我使用簡單的輸入文本中的一個時,工作正常。我得到/返回服務中的值沒有問題。
但我需要從HTML表單發送圖像,在我的服務中接收它,然後將其保存在服務器上。
下面是我午餐查詢服務的部分。
print_r($_FILES);
//move_uploaded_file($_FILES["image1"]["tmp_name"], "Images/" . $_FILES["image1"]["name"]); when uncommented, This line actually works and save the image in my folder.
include("RestUtils.php");
$request = new RestRequestOperator('http://localhost:8080/REST/RestControler.php/user/1', 'POST', $_FILES);
$request->execute();
在我的服務中,我收到圖像信息,它們是tmp_name和名稱。當我嘗試使用move_uploaded_file保存圖像(以及正確的參數,它不起作用)。
我意識到有一種魔法會在圖像文件中保存一段「短時間」 。TMP文件夾時,我打電話給我的服務,圖像已被刪除
結束語:?我不知道是否可以將圖像發送到PHP REST API,他們保存他們的服務器上
編輯:我在服務中加入了
if(file_exists($_POST["image1"]["tmp_name"])){
echo "file EXISTS<br>";
}else echo "NOPE.<br>";
if(is_uploaded_file($_POST["image1"]["tmp_name"])){
echo "is_uploaded_file TRUE<br>";
}else echo "is_uploaded_file FALSE<br> .";
if(move_uploaded_file($_POST["image1"]["tmp_name"], "Images/" .$_POST["image1"]["name"])){
echo "move_uploaded_file SUCCESS ";
}else echo "NOT move_uploaded_file";
輸出s:file EXISTS,is_uploaded_file FALSE,NOT move_uploaded_file 這意味着該文件實際上仍然存在於服務中,但上傳的文件返回False,可能是因爲我在服務中使用POST數組而不是$ _FILES數組,這在我的空服務.......