PHP Checking if URL/File Exists... Return True/False

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Voodoo
    ♥ ♦ ♣ ♠
    • Sep 2002
    • 10600

    #1

    PHP Checking if URL/File Exists... Return True/False

    Code:
    <?php if(file_exists("http://www.site.com/images/$id/misc6.jpg")){ DO THIS }?>
    For some reason this doesn't seem to work. When I remove this check, the image shows up under { DO THIS }, however, the check doesn't seem to be returning a TRUE on this statement. What's the best way to return TRUE/FALSE for a remote image if it exists?

    EDIT: Note... $id is assigned above this statement.

    "I'm selflessly supporting the common good, but only coincidentally looking out for No.1."
  • Voodoo
    ♥ ♦ ♣ ♠
    • Sep 2002
    • 10600

    #2
    Bumpity Bump

    "I'm selflessly supporting the common good, but only coincidentally looking out for No.1."

    Comment

    • Varius
      Confirmed User
      • Jun 2004
      • 6890

      #3
      file_exists doesn't support checks on remote files.

      why not just try with fopen ?
      Skype variuscr - Email varius AT gmail

      Comment

      • Dirty D
        Confirmed User
        • May 2002
        • 4044

        #4
        also make sure

        allow_url_fopen is enabled

        (Dirty D has chicks in the pool on Labor Day, why am I commenting on someone else's code?)

        Dirty D - ICQ #1326843 - $1 Million Dollars of Bonus Money - 8,000+ FHG!
        Glory Hole Girlz - Crack Whore Confessions - Tampa Bukkake - Slut Wife Training - Fuck a Fan
        Electricity Play - Porn Video Drive - Theater Sluts - Skunk Riley - Ukraine Amateurs - Strapon Sessions

        Comment

        • mikeyddddd
          Viva la vulva!
          • Mar 2003
          • 16557

          #5
          Originally posted by Dirty D
          (Dirty D has chicks in the pool on Labor Day, why am I commenting on someone else's code?)
          Shrinkage


          Comment

          • calmlikeabomb
            Confirmed User
            • May 2004
            • 1323

            #6
            The most secure way to accomplish what you need is using cURL.

            Many will argue that enabling URL wrappers is a potential security risk within PHP. Just google and you'll find many articles. This is why that option is turned off by default in the configuration file.
            subarus.

            Comment

            • nation-x
              Confirmed User
              • Mar 2004
              • 5370

              #7
              if the file is an image every time... you can use getimagesize >> http://us2.php.net/getimagesize

              Code:
              [b]Example #3 getimagesize (URL)[/b]
              <?php
              $size = getimagesize("http://www.example.com/gifs/logo.gif");
              
              // if the file name has space in it, encode it properly
              $size = getimagesize("http://www.example.com/gifs/lo%20go.gif");
              
              ?>

              Comment

              • nation-x
                Confirmed User
                • Mar 2004
                • 5370

                #8
                Originally posted by calmlikeabomb
                The most secure way to accomplish what you need is using cURL.

                Many will argue that enabling URL wrappers is a potential security risk within PHP. Just google and you'll find many articles. This is why that option is turned off by default in the configuration file.
                allow_url_fopen isn't really a big risk... allow_url_include is.

                Comment

                • calmlikeabomb
                  Confirmed User
                  • May 2004
                  • 1323

                  #9
                  allow_url_include doesn't work unless allow_url_fopen is enabled, buddy.
                  subarus.

                  Comment

                  • nation-x
                    Confirmed User
                    • Mar 2004
                    • 5370

                    #10
                    Originally posted by calmlikeabomb
                    allow_url_include doesn't work unless allow_url_fopen is enabled, buddy.
                    They have separate flags... so it is possible to enable allow_url_fopen and turn off allow_url_include.

                    Comment

                    • Davy
                      Confirmed User
                      • Apr 2006
                      • 4323

                      #11
                      ---
                      ICQ 14-76-98 <-- I don't use this at all

                      Comment

                      • GrouchyAdmin
                        Now choke yourself!
                        • Apr 2006
                        • 12085

                        #12
                        This is incredibly basic shit. The following is a top-down-no-classes-used example, using cURL, as suggested above. Note the lack of RETURNTRANSFER as we're only checking the status code.

                        Code:
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL,"http://www.site.com/blah/");
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
                        curl_setopt($ch,CURLOPT_VERBOSE, FALSE);
                        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                        $pageData=curl_exec($ch);
                        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                        curl_close($ch);
                        
                        switch ($statusCode) {
                          case 404:
                            filemissing()...
                            break;
                          case 401:
                            authreq();
                            break;
                          default:
                           whatev();
                        }
                        Originally posted by nation-x
                        if the file is an image every time... you can use getimagesize >> http://us2.php.net/getimagesize
                        Do not use getimagesize() as a boolean for validation - that is VERY unsafe.
                        Last edited by GrouchyAdmin; 09-08-2009, 08:18 AM. Reason: getinagesize() is bad for all living things

                        Comment

                        • nation-x
                          Confirmed User
                          • Mar 2004
                          • 5370

                          #13
                          Originally posted by GrouchyAdmin
                          Do not use getimagesize() as a boolean for validation - that is VERY unsafe.
                          Please provide an example to prove your assertion... how exactly is this unsafe?

                          This is how I would use it.

                          Code:
                          $file_url = "http://www.site.com/images/{$id}/misc6.jpg";
                          $file_exists = is_array($file_url);
                          Last edited by nation-x; 09-08-2009, 12:32 PM.

                          Comment

                          • GrouchyAdmin
                            Now choke yourself!
                            • Apr 2006
                            • 12085

                            #14
                            Originally posted by nation-x
                            Please provide an example to prove your assertion... how exactly is this unsafe?[/code]
                            Generally, lazily coded image hosting scripts. The easiest way to abuse this I've actually seen in the wild. It starts with a valid GIF header, and has a PHP script as the payload. WIthout giving away too many details, file naming conventions and autonegotiation can cause you bigtime issues.

                            Comment

                            • nation-x
                              Confirmed User
                              • Mar 2004
                              • 5370

                              #15
                              should have been:
                              Code:
                              $file_url = "http://www.site.com/images/{$id}/misc6.jpg";
                              $file_exists = is_array(getimagesize($file_url));

                              Comment

                              • MichaelP
                                Registered User
                                • Aug 2003
                                • 7124

                                #16
                                $yourfile = "full/path/to/your/file.php-jpg-gif-whatever" ;
                                IF (file_exists($yourfile)) {
                                print "Your File Does Existst" ; // TRUE
                                } ELSE {
                                print "Your file Doesn't exists" ; // FALSE
                                }
                                Last edited by MichaelP; 09-08-2009, 12:52 PM.

                                Comment

                                • Tom_PM
                                  Porn Meister
                                  • Feb 2005
                                  • 16443

                                  #17
                                  I ran into this issue long ago, but I think I solved it by realising that both domains I was working with were on the same server, doh!

                                  Good luck.
                                  43-922-863 Shut up and play your guitar.

                                  Comment

                                  • Killswitch - BANNED FOR LIFE

                                    #18
                                    Originally posted by MichaelP
                                    $yourfile = "full/path/to/your/file.php-jpg-gif-whatever" ;
                                    IF (file_exists($yourfile)) {
                                    print "Your File Does Existst" ; // TRUE
                                    } ELSE {
                                    print "Your file Doesn't exists" ; // FALSE
                                    }
                                    Not gonna work captain.

                                    Comment

                                    • ProG
                                      Confirmed User
                                      • Apr 2009
                                      • 1319

                                      #19
                                      Originally posted by Killswitch
                                      Not gonna work captain.
                                      I don't even think he read the thread.
                                      History will be kind to me for I intend to write it.

                                      Comment

                                      • GrouchyAdmin
                                        Now choke yourself!
                                        • Apr 2006
                                        • 12085

                                        #20
                                        Jeeesus peoples. This is a hack of a slightly-better-than-my-above for just checking for the files' existence, based upon code at PHP.net; I figured this thread should die a horrible death, so here ya go.

                                        Code:
                                        function http_file_exists($url=FALSE) {
                                          // Bad URL, bail.
                                          if (!(@parse_url($url))) return FALSE;
                                          // No cURL, bail.
                                          if (!(function_exists('curl_init'))) return FALSE;
                                          $ch = curl_init();
                                          curl_setopt($ch, CURLOPT_URL,$url);
                                          curl_setopt($ch, CURLOPT_NOBODY, 1);
                                          curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                                          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                          return (curl_exec($ch) !== FALSE) ? TRUE : FALSE;
                                        }
                                        Here's even a (shitty) working example:

                                        Code:
                                        $urltest = array ("http://www.google.com/logo.gif", "http://www.google.com/intl/en_ALL/images/logo.gif");
                                        
                                        foreach ($urltest as $url) {
                                          echo http_file_exists($url) ? "$url exists.\n" : "$url is a 404.\n";
                                        }
                                        exit;
                                        Code:
                                        http://www.google.com/logo.gif is a 404.
                                        http://www.google.com/intl/en_ALL/images/logo.gif exists.
                                        Can this go away now pls?

                                        Comment

                                        • nation-x
                                          Confirmed User
                                          • Mar 2004
                                          • 5370

                                          #21
                                          Originally posted by GrouchyAdmin
                                          Jeeesus peoples. Can this go away now pls?
                                          buttsecks???!!! :D

                                          Comment

                                          • GrouchyAdmin
                                            Now choke yourself!
                                            • Apr 2006
                                            • 12085

                                            #22
                                            Originally posted by nation-x
                                            buttsecks???!!! :D
                                            I gave at the office.

                                            Comment

                                            • Killswitch - BANNED FOR LIFE

                                              #23
                                              Originally posted by GrouchyAdmin
                                              Jeeesus peoples. This is a hack of a slightly-better-than-my-above for just checking for the files' existence, based upon code at PHP.net; I figured this thread should die a horrible death, so here ya go.

                                              Code:
                                              function http_file_exists($url=FALSE) {
                                                // Bad URL, bail.
                                                if (!(@parse_url($url))) return FALSE;
                                                // No cURL, bail.
                                                if (!(function_exists('curl_init'))) return FALSE;
                                                $ch = curl_init();
                                                curl_setopt($ch, CURLOPT_URL,$url);
                                                curl_setopt($ch, CURLOPT_NOBODY, 1);
                                                curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                                return (curl_exec($ch) !== FALSE) ? TRUE : FALSE;
                                              }
                                              Here's even a (shitty) working example:

                                              Code:
                                              $urltest = array ("http://www.google.com/logo.gif", "http://www.google.com/intl/en_ALL/images/logo.gif");
                                              
                                              foreach ($urltest as $url) {
                                                echo http_file_exists($url) ? "$url exists.\n" : "$url is a 404.\n";
                                              }
                                              exit;
                                              Code:
                                              http://www.google.com/logo.gif is a 404.
                                              http://www.google.com/intl/en_ALL/images/logo.gif exists.
                                              Can this go away now pls?
                                              No, I refuse to let it! BUMMMPPP!!

                                              Comment

                                              • GrouchyAdmin
                                                Now choke yourself!
                                                • Apr 2006
                                                • 12085

                                                #24
                                                Originally posted by Killswitch
                                                No, I refuse to let it! BUMMMPPP!!
                                                I cutchoo mang. I cutchoo so bad you wish I no cutchoo.


                                                ...and I'm gonna keep using a banned imagehost until this damn thing goes away. S'there!

                                                Comment

                                                • Killswitch - BANNED FOR LIFE

                                                  #25
                                                  Originally posted by GrouchyAdmin
                                                  I cutchoo mang. I cutchoo so bad you wish I no cutchoo.


                                                  ...and I'm gonna keep using a banned imagehost until this damn thing goes away. S'there!
                                                  I cut you back... I cut you deep, I cut you so deep, yo momma be like "WTF IS THAT!?!?".

                                                  Imageshack works on gfy.com, fail.

                                                  Comment

                                                  • GrouchyAdmin
                                                    Now choke yourself!
                                                    • Apr 2006
                                                    • 12085

                                                    #26
                                                    Originally posted by Killswitch
                                                    Imageshack works on gfy.com, fail.
                                                    LOS GROUCHY KICK YOUR ASS
                                                    LOS GROUCHY KICK YOUR FACE
                                                    LOS GROUCHY http_file_exists() IN TO OUTER SPACE

                                                    Dammit. It used to be banned as far back as.. two weeks ago.

                                                    Comment

                                                    • munki
                                                      Do Fun Shit.
                                                      • Dec 2004
                                                      • 13393

                                                      #27

                                                      I have the simplest tastes. I am always satisfied with the best.” -Oscar Wilde

                                                      Comment

                                                      • Killswitch - BANNED FOR LIFE

                                                        #28
                                                        Originally posted by GrouchyAdmin
                                                        LOS GROUCHY KICK YOUR ASS
                                                        LOS GROUCHY KICK YOUR FACE
                                                        LOS GROUCHY http_file_exists() IN TO OUTER SPACE

                                                        Dammit. It used to be banned as far back as.. two weeks ago.
                                                        LOS GROUCHY KICK ROCKS
                                                        LOS GROUCHY NO HURT
                                                        LOS GROUCHY http_file_exists() RETURN ERROR FOR SUCKAGE

                                                        Hmm, weird, that little smiley you posted from imageshack showed for me on gfy.com

                                                        Comment

                                                        • GrouchyAdmin
                                                          Now choke yourself!
                                                          • Apr 2006
                                                          • 12085

                                                          #29
                                                          Originally posted by Killswitch
                                                          LOS GROUCHY KICK ROCKS
                                                          LOS GROUCHY NO HURT
                                                          LOS GROUCHY http_file_exists() RETURN ERROR FOR SUCKAGE

                                                          Hmm, weird, that little smiley you posted from imageshack showed for me on gfy.com
                                                          Yeah, well, you're Retox_Josh.

                                                          Feel that burn, motherbitch. Feel it!? Mmmyeah. That's the stuff.

                                                          Comment

                                                          • quantum-x
                                                            Confirmed User
                                                            • Feb 2002
                                                            • 6863

                                                            #30
                                                            return (!file_get_contents('URL')?false:true);
                                                            Do I win ?
                                                            PrettyInCash.com - BoozedGFs.com - TeenGFs.com - JizzGFs.com- MilfUploads.com -

                                                            Comment

                                                            • Killswitch - BANNED FOR LIFE

                                                              #31
                                                              Originally posted by GrouchyAdmin
                                                              Yeah, well, you're Retox_Josh.

                                                              Feel that burn, motherbitch. Feel it!? Mmmyeah. That's the stuff.
                                                              Touché

                                                              You win this time my little pretty, but I will get you and your dog too!

                                                              Comment

                                                              • GrouchyAdmin
                                                                Now choke yourself!
                                                                • Apr 2006
                                                                • 12085

                                                                #32
                                                                Originally posted by quantum-x
                                                                return (!file_get_contents('URL')?false:true);
                                                                Do I win ?
                                                                No. Michael P wins for 'not reading the fucking thread at all', but nice try.

                                                                Originally posted by Killswitch
                                                                Touché

                                                                You win this time my little pretty, but I will get you and your dog too!
                                                                Surrender Grouchy, my ass.

                                                                Comment

                                                                • quantum-x
                                                                  Confirmed User
                                                                  • Feb 2002
                                                                  • 6863

                                                                  #33
                                                                  Originally posted by GrouchyAdmin
                                                                  No. Michael P wins for 'not reading the fucking thread at all', but nice try.



                                                                  Surrender Grouchy, my ass.
                                                                  Damnit .
                                                                  PrettyInCash.com - BoozedGFs.com - TeenGFs.com - JizzGFs.com- MilfUploads.com -

                                                                  Comment

                                                                  • munki
                                                                    Do Fun Shit.
                                                                    • Dec 2004
                                                                    • 13393

                                                                    #34

                                                                    I have the simplest tastes. I am always satisfied with the best.” -Oscar Wilde

                                                                    Comment

                                                                    • GrouchyAdmin
                                                                      Now choke yourself!
                                                                      • Apr 2006
                                                                      • 12085

                                                                      #35
                                                                      Originally posted by quantum-x
                                                                      Damnit .
                                                                      You do get a few points for ternary abuse, though.

                                                                      Comment

                                                                      • quantum-x
                                                                        Confirmed User
                                                                        • Feb 2002
                                                                        • 6863

                                                                        #36
                                                                        Originally posted by GrouchyAdmin
                                                                        You do get a few points for ternary abuse, though.
                                                                        If you cram it aaaalllll onto one line, if the script ever breaks, at least you know the line to debug
                                                                        PrettyInCash.com - BoozedGFs.com - TeenGFs.com - JizzGFs.com- MilfUploads.com -

                                                                        Comment

                                                                        Working...