Is there a way to upload a file from a client side to the server using REST using PHP,
I am trying to use the below code, and it is not working from me.
<?php
$file_to_upload = array('file_contents'=>'@c:\\test.txt');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/upload.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSER, TRUE);
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_POST,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload);
curl_exec($ch) or die( curl_error($ch) );
$error = curl_error($ch);
curl_close ($ch);
echo " Server response: ".$result;
echo " Curl Error: ".$error;
?>
and my upload.php
$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_POST['file']['name']);
echo $uploadfile;
echo "\n";
echo '<pre>';
echo $_POST['file']['tmp_name'];
if (move_uploaded_file($_POST['file']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
echo "\n<hr />\n";
print_r($_POST);
print "</pr" . "e>\n";
?>
php5.5+
Try this.
index.php
and upload.php in http://www.restServiceHost.com/file3