4

Im trying to download images from remote server, resize and then save it local machine.

To do this I use a WideImage.

<?php include_once($_SERVER['DOCUMENT_ROOT'].'libraries/wideimage/index.php'); include_once($_SERVER['DOCUMENT_ROOT'].'query.php'); do { wideImage::load($row_getImages['remote'])->resize(360, 206, 'outside')->saveToFile($_SERVER['DOCUMENT_ROOT'].$row_getImages['local']);} while ($row_getImages = mysql_fetch_assoc($getImages)); ?> 

This works most of the time. But it has a fatal flaw.

If for some reason one of these images is not available or doesn't exists. Wideimage throws a fatal error. Preventing any further images that may exist from downloading.

I have tried checking file exists like this

 do { if(file_exists($row_getImages['remote'])){ wideImage::load($row_getImages['remote'])->resize(360, 206, 'outside')->saveToFile($_SERVER['DOCUMENT_ROOT'].$row_getImages['local']);} } while ($row_getImages = mysql_fetch_assoc($getImages)); 

But this doesn't work.

What am I doing wrong??

Thanks

4
  • Why does the file_exists() check work and how is $row_getImages defined? Commented May 6, 2011 at 14:56
  • $row_getImages is defined in query.php which is included. file_exists() doesn't seem to work at all. Commented May 6, 2011 at 15:00
  • Not sure what happen to answer that mentioned "try" and "catch" but it worked. Thanks everyone. Commented May 6, 2011 at 15:11
  • Can you add your try and catch solution to your post so others can benefit from it too? Commented May 9, 2013 at 13:48

3 Answers 3

5

According to this page, file_exists cannot check for remote files. Someone suggested there, in the comments, that they use fopen as a workaround:

<?php function fileExists($path){ return (@fopen($path,"r")==true); } ?> 
Sign up to request clarification or add additional context in comments.

Comments

0

You could check via CURL:

$curl = curl_init('http://example.com/my_image.jpg'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_NOBODY, TRUE); $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if($httpcode < 400) { // do stuff } 

1 Comment

Not working. Always "$httpcode" is less than 400, file exists or not does not. Very bad post.
0

After some digging around the net I decided on requesting the HTTP headers, instead of a CURL request, as apparently it's less overhead.

This is an adaption of Nick's comment from the PHP forums at: http://php.net/manual/en/function.get-headers.php

function get_http_response_code($theURL) { $headers = get_headers($theURL); return substr($headers[0], 9, 3); } $URL = htmlspecialchars($postURL); $statusCode = intval(get_http_response_code($URL)); if($statusCode == 200) { // 200 = ok echo '<img src="'.htmlspecialchars($URL).'" alt="Image: '.htmlspecialchars($URL).'" />'; } else { echo '<img src="/Img/noPhoto.jpg" alt="This remote image link is broken" />'; } 

Nick calls this function "a quick and nasty solution", although it is working well for me :-)

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.