I have written a script to upload an image file to a server using file_get_contents(); this works when using a relative file path eg. /var/test/image1.jpg and also when I use a remote url eg. http://domain.com/test/image1.jpg.
However, it does not work for local paths ie. file://C:/Users/me/Pictures/image1.jpg. I get, "file_get_contents(file://C:/Users/me/Pictures/image1.jpg) [function.file-get-contents]: failed to open stream: No such file or directory" or "remote host file access not supported".
I really need this to work for local paths so that remote clients can upload their local images to my server via an API. I therefore cannot use a form and need to use the PUT method specified here ([http://www.php.net/manual/en/features.file-upload.put-method.php][1]) but how to I access the file?
The code for my API is below but am trying to figure out how to get the file contents from a clients local machine:
if($_SERVER['REQUEST_METHOD'] == 'PUT') {
$input = file_get_contents("php://input"); //filepath passed from client in XML
//Convert the XML to an object we can deal with
$xml = simplexml_load_string($input);
$file_path = $xml->image->filepath;
if (file_exists($file_path) ){
$file_data = file_get_contents($file_path);
//Initialise the Curl Instance
$ch = curl_init();
//Set our Curl Options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);
//Execute the request
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//Close the Handle
curl_close($ch);
}