I made a REST Service which is heavily based on this tutorial. I also made a REST Request library which is heavily based on this tutorial. (Basically, a bunch of switch
on $_SERVER['REQUEST_METHOD']
). the api also make requests with 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);
}
I have 2 simple HTML forms, one with a get method and one for the post method. When i use one of them with simple input text, works fine. I get/return the values in the service with no problem.
But I need to send an image from a HTML form, receive it in my service and then save it on the server.
Here is the part where I lunch make the query to the service.
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();
In my service, I receive the image infos , which are the tmp_name and the name. When I try to save the image with move_uploaded_file( and the right parameters , it doesn't work.
I realise there is some sort of magic going there with the image file saved for a 'short laps of time' in the tmp folder. when i call my service, the image is already removed ?
Wrap up : I wonder if it is possible to send an image to a PHP REST API and they save it on their server.
EDIT : I added in the service.
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";
outputs : file EXISTS , is_uploaded_file FALSE, NOT move_uploaded_file which means the file actually still exist in the service but is uploaded file returns falses, probably beacause I use the POST array in the service instead of the $_FILES array, which is empty in my service.......
Found it, works on local wamp PHP 5.3.4 !
public static function processRequest()
{
case 'post':
if(move_uploaded_file($_FILES["uploaded_file"]["tmp_name"] , "Images/" . $_FILES["uploaded_file"]["name"] )){
echo "move_uploaded_file SUCCESS ";
......................................
}
......................................
protected function executePost ($ch)
{
$tmpfile = $_FILES['image1']['tmp_name'];
$filename = basename($_FILES['image1']['name']);
$data = array(
'uploaded_file' => '@' . $tmpfile . ';filename='.$filename,
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//no need httpheaders
$this->doExecute($ch);
}
Thanks. Dave
Check the encoding type of your POST. In a form it needs to be "multipart/form-data" so I'm assuming your POST REST call will need to have a similar encoding type for the file upload to work properly.