RickyF Posted July 13, 2007 Share Posted July 13, 2007 Hello, Is there a way to check if an image exists, using fsock or similar? Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/ Share on other sites More sharing options...
jitesh Posted July 13, 2007 Share Posted July 13, 2007 <?php $filename = '/path/to/foo.jpg'; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297322 Share on other sites More sharing options...
chocopi Posted July 13, 2007 Share Posted July 13, 2007 why dont you just use file_exists() ??? <?php $filename = '/path/to/foo.txt'; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> or i think this might work (this is untested): <?php $fp = fsockopen("www.example.com"); if (!$fp) { echo "File does not exist<br />\n"; } else { echo "File exist<br />\n"; fclose($fp); } ?> Those are both off of php.net ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297323 Share on other sites More sharing options...
infid3l Posted July 13, 2007 Share Posted July 13, 2007 <?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "HEAD /pics/image.jpg HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $data .= fgets($fp, 128); } fclose($fp); } if(eregi("404 Not Found\r\n", $data)) { $exists = false; } else { $exists = true; } ?> To see if a off-server file exists (chocopi's code only checks if a server is online, missing arguments). Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297327 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 <?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "HEAD /pics/image.jpg HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $data .= fgets($fp, 128); } fclose($fp); } if(eregi("404 Not Found\r\n", $data)) { $exists = false; } else { $exists = true; } ?> To see if a off-server file exists (chocopi's code only checks if a server is online, missing arguments). Hello, sorry i didnt make my request clear enough!! I required off server image checking, thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297350 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 The image string is $imageurl The script seems to try and use www.example.com and then the image location on the domain seperately, can you make it so that $imageurl is the image thats checked to see if it exists? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297361 Share on other sites More sharing options...
infid3l Posted July 13, 2007 Share Posted July 13, 2007 The image string is $imageurl The script seems to try and use www.example.com and then the image location on the domain seperately, can you make it so that $imageurl is the image thats checked to see if it exists? Thanks! Change www.example.com to the host, ie. www.photobucket.com. If you want a variable for the image, do: $out = "HEAD /pics/$imageurl HTTP/1.1\r\n"; or something similar Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297363 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 There are two strings, one is imageurl the other is weburl An example of a users input would be: imageurl - http://www.test.com/image.jpg weburl - http://www.test.com http:// is posted - i think this may cause problems, is there a way to send whatever comes after http://? so that only www.test.com is sent to the image checker Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297373 Share on other sites More sharing options...
infid3l Posted July 13, 2007 Share Posted July 13, 2007 There are two strings, one is imageurl the other is weburl An example of a users input would be: imageurl - http://www.test.com/image.jpg weburl - http://www.test.com http:// is posted - i think this may cause problems, is there a way to send whatever comes after http://? so that only www.test.com is sent to the image checker $url = "http://www.example.com/pic.jpg"; $url = str_ireplace("http://", null, $url); // www.example.com/pic.jpg $page = substr($url, strpos($url, "/"), strlen($url)); // /pic.jpg $url = str_replace($page, null, $url); // www.example.com edit: You can also do (recommended): $url = "http://www.example.com/pic.jpg"; $parts = explode("/", str_ireplace("http://", null, $url), 2); $url = $parts[0]; // www.example.com $page = $parts[1]; // pic.jpg Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297375 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 Would this be correct? $url = "$imageurl"; $parts = explode("/", str_ireplace("http://", null, $url), 2); $url = $parts[0]; $page = $parts[1]; $fp = fsockopen("$parts[0]", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "HEAD $parts[0] HTTP/1.1\r\n"; $out .= "Host: $parts[1]\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $data .= fgets($fp, 128); } fclose($fp); } if(eregi("404 Not Found\r\n", $data)) { die ("image doesnt exist"); } Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297378 Share on other sites More sharing options...
infid3l Posted July 13, 2007 Share Posted July 13, 2007 Would this be correct? $url = "$imageurl"; $url = str_ireplace("http://", null, $url); $page = strsub($url, strpos($url, "/"), strlen($url); $url = str_replace($page, null, $url); $fp = fsockopen("$page", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "HEAD $page HTTP/1.1\r\n"; $out .= "Host: $weburl\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $data .= fgets($fp, 128); } fclose($fp); } if(eregi("404 Not Found\r\n", $data)) { die ("image doesnt exist"); } $parts = explode("/", str_ireplace("http://", null, $imgurl), 2); $url = $parts[0]; // www.example.com $page = "/".$parts[1]; // pic.jpg $fp = fsockopen($url, 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "HEAD $page HTTP/1.1\r\n"; $out .= "Host: $url\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $data .= fgets($fp, 128); } fclose($fp); } if(eregi("404 Not Found\r\n", $data)) { die ("image doesnt exist"); } Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297379 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 Hi, Sorry i tried again with your second recommended code. Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297382 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 Fatal error: Call to undefined function: str_ireplace() on line 25 Line 25: $parts = explode("/", str_ireplace("http://", null, $url), 2); Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297383 Share on other sites More sharing options...
infid3l Posted July 13, 2007 Share Posted July 13, 2007 Fatal error: Call to undefined function: str_ireplace() on line 25 Line 25: $parts = explode("/", str_ireplace("http://", null, $url), 2); Ah, old PHP version. Use this instead: $parts = explode("/", str_replace("http://", null, strtolower($imgurl)), 2); Replace it in the code I posted above. Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297385 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 If the website doesnt exist it seems to say: Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/galaxy/public_html/plug/index.php on line 30 Warning: fsockopen() [function.fsockopen]: unable to connect to boasdasd.com:80 in /home/galaxy/public_html/plug/index.php on line 30 Permission denied (13) But i have instructed the script to die and show an error? Could you give the code a quick check to make sure the strings are in the right place etc $url = "$imageurl"; $parts = explode("/", str_replace("http://", null, strtolower($url)), 2); $url = $parts[0]; $page = $parts[1]; $fp = fsockopen("$parts[0]", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "HEAD $parts[1] HTTP/1.1\r\n"; $out .= "Host: $parts[0]\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $data .= fgets($fp, 128); } fclose($fp); } if(eregi("404 Not Found\r\n", $data)) { die ("image doesnt exist"); } Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297391 Share on other sites More sharing options...
infid3l Posted July 13, 2007 Share Posted July 13, 2007 Fixed your errors, too tired to list them. $url = $imageurl; $parts = explode("/", str_replace("http://", null, strtolower($url)), 2); $fp = fsockopen($parts[0], 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "HEAD ".$parts[1]." HTTP/1.1\r\n"; $out .= "Host: ".$parts[0]."\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $data .= fgets($fp, 128); } fclose($fp); } if(eregi("404 Not Found\r\n", $data)) { die ("image doesnt exist"); } should work Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297398 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 Posting a working image/website works, it posts as it should, but when the image/website is non existant, its showing: Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/galaxy/public_html/plug/index.php on line 28 Warning: fsockopen() [function.fsockopen]: unable to connect to gsdfsdfsdd.com:80 on line 28 Permission denied (13) Line 28: $fp = fsockopen($parts[0], 80, $errno, $errstr, 30); Can you not hide this output and make it show image doesnt exist? Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297403 Share on other sites More sharing options...
MadTechie Posted July 13, 2007 Share Posted July 13, 2007 Why not this <?php $url = "http://us2.php.net/images/php.gif"; if( !is_numeric(xfile_404($url)) ) { echo "Found<br>"; echo "<img src='$url'>"; } function xfile_404($file){ $file = ereg_replace(' +', '%20', trim($file)); if(substr($file, 0, 7) !== "http://") { $file = "http://" . $file; } $domain = substr($file, 7); $domain_ext = strtoupper(array_pop(explode('.', substr($domain, 0, strpos($domain, '/'))))); $file_headers = @get_headers($file); if(!$file_headers){ return 3; break; } if($file_headers[0] == 'HTTP/1.1 404 Not Found') { return 404; }else{ return ereg_replace('%20', ' ', $file); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297415 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 Is it not possible to modify the below to work as required? $url = $imageurl; $parts = explode("/", str_replace("http://", null, strtolower($url)), 2); $fp = fsockopen($parts[0], 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "HEAD ".$parts[1]." HTTP/1.1\r\n"; $out .= "Host: ".$parts[0]."\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $data .= fgets($fp, 128); } fclose($fp); } if(eregi("404 Not Found\r\n", $data)) { die ("image doesnt exist"); } Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297429 Share on other sites More sharing options...
infid3l Posted July 13, 2007 Share Posted July 13, 2007 MadTechie's alternative is better, but if you must: $fp = @fsockopen($parts[0], 80, $errno, $errstr, 30); Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297431 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 I am a php newbie, i dont know how to implement this code, i tried but it didnt work. Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297432 Share on other sites More sharing options...
infid3l Posted July 13, 2007 Share Posted July 13, 2007 I am a php newbie, i dont know how to implement this code, i tried but it didnt work. Put @ before the fsockopen(). Does it work when the image does exist? Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297433 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 It says Success (0) if the image is not existant, and says nothing but posts the data if its real. (Your script) Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297437 Share on other sites More sharing options...
MadTechie Posted July 13, 2007 Share Posted July 13, 2007 To add your code <?php //add to code $url = "http://us2.php.net/images/php.gif"; $found = rfile_exists($url); //example if($found) { echo "found"; }else{ echo "not found"; } //Add to the very end of your file. <?php function rfile_exists($url) { if( !is_numeric(xfile_404($url)) ) { return true; }else{ return false; } } function xfile_404($file){ $file = ereg_replace(' +', '%20', trim($file)); if(substr($file, 0, 7) !== "http://") { $file = "http://" . $file; } $domain = substr($file, 7); $file_headers = @get_headers($file); if(!$file_headers){ return 3; break; } if($file_headers[0] == 'HTTP/1.1 404 Not Found') { return 404; }else{ return ereg_replace('%20', ' ', $file); } } ?> Posting your code may help us Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297443 Share on other sites More sharing options...
RickyF Posted July 13, 2007 Author Share Posted July 13, 2007 Fatal error: Call to undefined function: rfile_exists() on line 4 Line 4: $found = rfile_exists($url); Quote Link to comment https://forums.phpfreaks.com/topic/59801-solved-checking-if-an-image-exists-using-fsock/#findComment-297448 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.