Web Analytics Made Easy -
StatCounter Image Exists? - CodingForum

Announcement

Collapse
No announcement yet.

Image Exists?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Image Exists?

    I need to check to see if an image exists on a remote server and if it does, show it. This is what I have tried:


    $imgCheck=readfile("http://$domainid:8900/$courseid/images/banner.jpg");
    if($imgCheck) echo "<img border=0 src=\"http://$domainid:8900/$courseid/images/banner.jpg\" width=\"550\" height=\"80\" alt=\"banner\">";


    So far I can't get it to work out. What I am I missing

  • #2
    PHP Code:
    $fp = @fopen("http://$domainid:8900/$courseid/images/banner.jpg","r");
    if (
    $fp) {
        echo 
    '<img border=0 src="http://'.$domainid.':8900/'.$courseid.'/images/banner.jpg" width="550" height="80" alt="banner">';
    } else { echo 
    'Resource not valid'; } 
    Last edited by Feyd; Jul 8, 2002, 05:21 PM.
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society

    Comment


    • #3
      Hmmm...When I try that it tells me it is not a valid resourse yet the resource is there. When I take out the @ to check for error messages I get:

      Warning: fopen("http://161.28.224.167:8900/BMC_2250IN02/images/banner.jpg","r") - Success in /web/htdocs/uvnet/PHP/WebCT/openfeedback.php on line 45

      If I just use:

      echo '<img border=0 src="http://'.$domainid.':8900/'.$courseid.'/images/banner.jpg" width="550" height="80" alt="banner">';

      where I know there is an image it will show the image, yet if I try to detect it using fopen or readfile it fails. We have several hundred places to check and some of them have the new banner set up and some of them don't, I just want to write one page to deal with it all.

      Comment


      • #4
        Weird...works for me...wait, you've got password protection on that dir. Are all of these on the same box/ip?
        Moderator, Perl/CGI Forum
        shadowstorm.net - subvert society

        Comment


        • #5
          Or you could do

          PHP Code:
          if($img = @GetImageSize('http://'.$domainid.':8900/'.$courseid.'/images/banner.jpg')) {
              echo 
          'image exists';
          } else {
              echo 
          'image does not exist';

          But you are still going to get into the user auth problem (which is why it is telling you it doesn't exist) unless you hardcode the auth into this script (which could be bad juju), or open the file directly on the server file level as opposed to ip/URL. (or you could wrap it in a login)
          Moderator, Perl/CGI Forum
          shadowstorm.net - subvert society

          Comment


          • #6
            Unfortunently they are on diferent servers. When I try this:

            if($img = @GetImageSize('http://Username:[email protected]'.$domainid.':8900/'.$courseid.'/images/banner.jpg')) {
            echo 'image exists';
            } else {
            echo 'image does not exist';
            }

            it still fails. However if I just enter this into the adress bar it works:

            http://Username:[email protected]:8900/BMC_2250IN02/images/banner.jpg
            Last edited by JohnKrutsch; Jul 8, 2002, 06:34 PM.

            Comment


            • #7
              have a play with the snoopy class ...




              this just grabs the image contents, but you should be able to elicit a true or false out of this...

              PHP Code:
              <?
              include "Snoopy.class.inc";
                  
              $snoopy = new Snoopy;
                  
              //$snoopy->user = "joe";
                  //$snoopy->pass = "bloe";
                  
                  
              if($snoopy->fetch("http://www.firepages.com.au/img/v4.jpg"))
                  {
                      echo 
              "response code: ".$snoopy->response_code."<br>\n";
                      echo 
              $snoopy->results;
                  
                  }
                  else
                      echo 
              "error fetching document: ".$snoopy->error."\n";
              ?>
              I am not sure if getimagesize() works on remote files and I assume that you need fopen wrappers turned on if it actually does.

              Another interested point, you would need to do something with the code above to chek the content (perhaps the returned headers?) as when you attempt to open a file that does not exists, you still get an OK response, I assume as the web-server dishes out a 404 if the file is not found, I know that snoopy can return just the headers though and this should do the trick??
              resistance is...

              MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

              Comment


              • #8
                this should work ... you just have to check the $arr array for 'image/jpeg' etc.

                PHP Code:
                <?
                include "Snoopy.class.inc";
                    
                $snoopy = new Snoopy;
                    
                    
                //$snoopy->user = "joe";
                    //$snoopy->pass = "bloe";
                    
                    
                if($snoopy->fetch("http://www.firepages.com.au/img/v4.jpg"))
                    {
                        echo 
                "response code: ".$snoopy->response_code."<br>\n";
                        
                $arr=$snoopy->headers;
                        while(list(
                $key,$var)=each($arr)){echo "$key , $var<br>";}
                }
                    else
                        echo 
                "error fetching document: ".$snoopy->error."\n";
                ?>
                resistance is...

                MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

                Comment

                Working...
                X