I am trying to upload a file using a RESTful web service as follows:
$filename = "pathtofile/testfile.txt";
$handle = fopen($filename, "r");
$filecontents = fread($handle, filesize($filename));
fclose($handle);
$data = array('name' => 'testfile.txt', 'file' => $filecontents);
$client = curl_init($url);
curl_setopt($client, CURLOPT_POST, true);
curl_setopt($client, CURLOPT_POSTFIELDS, $data);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
curl_close($client);
But I keep gettig file is empty as a response for this request.
I tried also sending the file path like:
$data = array('name' => 'testfile.txt', 'file' => 'pathtofile/testfile.txt');
curl_setopt($client, CURLOPT_POSTFIELDS, $data);
Or: just sending the contents of the file only like:
curl_setopt($client, CURLOPT_POSTFIELDS, $filecontents);
But the same response: file is empty.
Note that: the file exists and not empty, and I am just trying to only upload the file no additional fields .
I saw this post , but the same problem, any ideas?