0

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);
}
4
  • strange when people downvote and don't comment... upvoted just because of that. Commented Nov 15, 2012 at 12:02
  • 1
    what are you trying to do? have you uploaded the image and is trying to give it to the user? it looks to me like you're trying to access the client-side file when using file://C:/Users... Commented Nov 15, 2012 at 12:06
  • @Asken, thanks was a little confused by the downvote myself! I am trying to upload a file from a remote computer without using a form. I am using Curl. Commented Nov 15, 2012 at 12:32
  • and an example of a curl might be? Commented Nov 15, 2012 at 12:42

2 Answers 2

4

You shouldn't specify file:// To access local paths you can directly do

file_get_contents('C:/Users/me/Pictures/image1.jpg');

You can also navigate relative to current directory, using ../ based navigation like below

file_get_contents('../../../somefile.xyz');

EDIT: If you are trying to access a client-side file, i.e. remote paths that way, that wouldn't work. You need something like HTTP, FTP etc to transfer your file over. Simply giving a local file-system address isn't sufficient.

The end-point needs to somehow make the file available (HTTP/FTP server) or you need to get the client to upload it via the browser and an <input> field.

Sign up to request clarification or add additional context in comments.

7 Comments

@LeeTee What do you mean local paths? If that is a remote computer you are accessing, file_get_contents can't fetch it for you. If it is a local access within your server, drop the file://.
checked permissions on that file and its says SYSTEM full permission. I assume thats correct?
I want access files on a remote computer yes. If I cannot do this then how can I upload files without a form?
@LeeTee You can't just pull files off a remote computer, unless (a) The file is exposed to the web (webserver installed at remote end-point) or (b) You authenticate, and fetch via FTP (needs FTP server running at remote endpoint), or (c) The user at the remote computer uploads it.
Its an API, therefore the users will upload files but not with a form. They will need to specify the local file path to a file and I will somehow upload it using Curl.
|
1

There's no way that you can use file_get_contents() to open any files from the remote client's computer, unless they run it locally (they would also need to set up PHP for that) and then somehow submit it to your server.

file_get_contents() allows you to open files that are on the same machine with your script, not on different ones

You can take a look at http://www.tizag.com/phpT/fileupload.php for an easy example of file uploaders

7 Comments

Yes it is possible. Check the examples in the documentation php.net/manual/en/function.file-get-contents.php
ok, so how do I allows remote users to upload files to my server via an API?
how do I get them to submit it my server without a form?
@EdsonMedina i'm sorry but i really can't find a proper working example of what LeeTee is trying to do. I admit that i misspoke when i said that you can't open file other than on the server. but all the examples in the documentation refer to local files and files from weblinks. none refers to files opened from a client application
@LeeTee you need the form to submit to your server. but you can only access the file on the server side after it was submitted and fully uploaded. check the example at link
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.