How to find out if a file exists on a remote / local server? file_exists(), fopen()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chromis
    New Member
    • Jan 2008
    • 113

    How to find out if a file exists on a remote / local server? file_exists(), fopen()?

    Hi there,

    This has probably been asked a million times but having looked through the PHP manual for the file_exists() function I can't seem to get the right code for what I want to do.

    I have a typical scenario of wanting to check for an image files existence before displaying, I have foubd the following code to do this but it always returns false:

    Code:
    $imageURL = "http://website.com/images/image1.jpg";
    										
    $handle = @fopen($imageURL, 'r');
    if ($handle===false) { 
      echo "image does not exist";
    }
    else {
      echo "image exists";
      fclose($handle);
    }
    What is the best way to find out if an image exists on a remote or local server?

    Thanks,

    Chromis
  • abhishektiwari
    New Member
    • Feb 2008
    • 4

    #2
    hi Chromis,

    PHP file_exists() function takes file system path. as an argument.

    Comment

    • chromis
      New Member
      • Jan 2008
      • 113

      #3
      Originally posted by abhishektiwari
      hi Chromis,

      PHP file_exists() function takes file system path. as an argument.
      Hi abhishektiwari,

      Thanks for the reply, I wish to check if a file exists using an http address, i am thinking file_exists() is not the right function to use, what function should I use?

      Thanks,

      Chromis

      Comment

      • nomad
        Recognized Expert Contributor
        • Mar 2007
        • 664

        #4
        Originally posted by chromis
        Hi abhishektiwari,

        Thanks for the reply, I wish to check if a file exists using an http address, i am thinking file_exists() is not the right function to use, what function should I use?

        Thanks,

        Chromis
        yes you can use file_exist

        might look something like this


        if (file_exists("t est.txt")) {
        echo "test.txt is a file!";
        }


        nomad
        Last edited by nomad; Feb 19 '08, 03:38 PM. Reason: was going to add more details on the functions

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by chromis
          Hi there,

          This has probably been asked a million times but having looked through the PHP manual for the file_exists() function I can't seem to get the right code for what I want to do.

          I have a typical scenario of wanting to check for an image files existence before displaying, I have foubd the following code to do this but it always returns false:

          Code:
          $imageURL = "http://website.com/images/image1.jpg";
          										
          $handle = @fopen($imageURL, 'r');
          if ($handle===false) { 
            echo "image does not exist";
          }
          else {
            echo "image exists";
            fclose($handle);
          }
          What is the best way to find out if an image exists on a remote or local server?

          Thanks,

          Chromis
          file_exists() is perfect to use! Is it not obvious? You're wanting to check if a file exists ... file_exists?

          ;)

          Comment

          • chromis
            New Member
            • Jan 2008
            • 113

            #6
            Originally posted by markusn00b
            file_exists() is perfect to use! Is it not obvious? You're wanting to check if a file exists ... file_exists?

            ;)
            Yeah it's completely obvious but what is perhaps not so obvious in my explanation is that I want to find out whether a file at a specific URL exists. file_exists() in PHP 5 does not accept URLs only local path names.

            Having tryed a number of different solutions only this one worked for me:

            [code=php]
            function url_exists($url ){
            $url = str_replace("ht tp://", "", $url);
            if (strstr($url, "/")) {
            $url = explode("/", $url, 2);
            $url[1] = "/".$url[1];
            } else {
            $url = array($url, "/");
            }

            $fh = fsockopen($url[0], 80);
            if ($fh) {
            fputs($fh,"GET ".$url[1]." HTTP/1.1\nHost:".$ur l[0]."\n\n");
            if (fread($fh, 22) == "HTTP/1.1 404 Not Found") { return FALSE; }
            else { return TRUE; }

            } else { return FALSE;}
            }
            [/code]

            Thanks for your input though,

            Chromis

            Comment

            Working...