1

I have one remote url which outputs the image The url is in format like this

http://domain.com/my_file/view/<file_id>/FULL/ 

In the url "my_file" is a controller name, "view" is a function name and the other two are the parameters

If I hit this url in browser it shows me image

I want to take that image in my projects folder

I have tried with file_get_contents but it gives me warning with 404

How can I achieve that?

8
  • Give a real url maybe? Commented Feb 27, 2014 at 8:30
  • 1
    If the image is 404ing, then it either doesn't exist, or any clauses you've put in place to limit access to it (such as leech protection, limits based on user-agent / referrer) are not being met. Is this image on a server / host which you control? Commented Feb 27, 2014 at 8:31
  • give your real full image url your url must be domain.com/my_file/view/<file_id>/FULL/image.png or must be point to image name Commented Feb 27, 2014 at 8:32
  • You may need to parse the site for the real image link and use that instead of the link you provided Commented Feb 27, 2014 at 8:32
  • 1
    @SnehalS Your browser might follow some redirections. file_get_contents() won't. If you gave us the full URL it would be way simpler, we cannot just guess what's the real problem here. Commented Feb 27, 2014 at 8:36

3 Answers 3

2
$img=file_get_contents('http://example.com/image/test.jpg'); file_put_contents('/your/project/folder/imgname.jpg',$img); 

This works only if allow_url_fopen is set to 1 in your php.ini file. If you can change this value, enable it and you're done.

Another option is CURL. Check if this module is enabled in your PHP configuration.

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.com/image/test.jpg'); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_HEADER , 0); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $result = @curl_exec($ch); $curl_err = curl_error($ch); curl_close($ch); if (empty($curl_err)) { file_put_contents('/your/project/folder/imgname.jpg',$result); } 

If CURL is not enabled, your chance is to write a simple HTTP client like this:

 $buf=''; $fp = fsockopen('example.com',80); fputs($fp, "GET /image/test.jpg HTTP/1.1\n" ); fputs($fp, "Host: example.com\n" ); fputs($fp, "Connection: close\n\n" ); while (!feof($fp)) { $buf .= fgets($fp,128); } fclose($fp); file_put_contents('/your/project/folder/imgname.jpg',$buf); 
Sign up to request clarification or add additional context in comments.

Comments

0

Use Curl for this:

function curlFile($url) {

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); curl_close($ch); return $data; } function readurl() { $url="http://domain.com/my_file/view/<file_id>/FULL/"; curlFile($url); } echo readurl(); 

Comments

0

If you are trying

$image = file_get_contents('http://domain.com/my_file/view/<file_id>/FULL/'); if($image) file_put_contents('some/folder/<file_id>'); 

and it does not work, it probably means either:

  1. That there is access control that prevents it on the remote server. In that case, you must set the appropriate cookies. I suggest using curl to do that.
  2. The image path is wrong; try to view source when navigating to http://domain.com/my_file/view/<file_id>/FULL/ using your browser.
  3. The trailing / in your URL should not be there, e.g. http://domain.com/my_file/view/<file_id>/FULL?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.