Jump to content

[SOLVED] Checking if an image exists using fsock?


RickyF

Recommended Posts

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

Link to comment
Share on other sites

<?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).

Link to comment
Share on other sites

<?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! ;D

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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");
}

 

 

Link to comment
Share on other sites

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");
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);
        }
}
?>

Link to comment
Share on other sites

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");
}

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.