Results 1 to 6 of 6

Thread: file_exist alternative (returning false)

  1. #1
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Post file_exist alternative (returning false)

    Hello, I am trying to create a script that will detect if a URL exists or not. I was using the function, file_exists, but every single time it returns false, regardless if the URL exists or not. Is there any alternative to this?

    THe code is as follows:
    PHP Code:
    <?php

    $file 
    'http://www.google.com/'

    if (file_exists($file)) {
    echo (
    'This URL exists!');
    }
    else {
    echo (
    'This URL is unavailable');
    }
    ?>

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JShor View Post
    Hello, I am trying to create a script that will detect if a URL exists or not. I was using the function, file_exists, but every single time it returns false, regardless if the URL exists or not.
    It will: the file_exists function requires stat family support to work with URI wrappers, but stat family support isn't provided for the HTTP and HTTPS protocols. The PHP manual would have told you this.

    Is there any alternative to this?
    Depends on what URIs will be used and what you think constitutes existence. If only HTTP URIs are involved, you can send a HTTP request to the host and check the response, though there are a lot of possibilities to consider than just a 200 status.
    Mike

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You're missing a semicolon -- ; on the third line.

    This may have to do with remote files. There should be a setting you can turn on to allow opening remote files.

    One alternative is to try include('some_url'), and use an output buffer. This is a lot of extra work, though.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Another thing you could try is using fsockopen to connect to the certain site you are looking for, then see if you get a respond from there. Beware when doing this, though, as it could cause the script to lag out.

    Hope this helps nonetheless.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    There are lots of different ways to do this. The best way will depend on what you are going to do once you find if it exists. Here's one solution.
    PHP Code:
    $url 'http://www.google.com';
    $handle fopen($url,'r');
    if(
    $handle !== false){
       echo 
    'Exists';
    }
    else{
       echo 
    "Doesn't";


  6. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    gotcha! it works blm, thx!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •