How to check if link exists with PHP?

Discussion in 'PHP' started by shubhamjain, Nov 24, 2009.

  1. #1
    Well, i know that there 100 methods to do that but the thing is that I want to check existence of files which might be as big as 100 MBS.

    So requesting the whole file and checking existence will be huge waste of bandwidth and time. Any way to solve my problem.?
     
    shubhamjain, Nov 24, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    JAY6390, Nov 24, 2009 IP
  3. papsyface

    papsyface Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In situations where you need more control (such as having to login first, having to specify the referer, etc) - I'd recommend using cURL. Basically, you use the "HEAD" request (rather than GET/POST) to retrieve the response headers for the URL in question. So with cURL, you could use something like this:

    
    function response_code($url) {
    	$ch = curl_init($url);
    	$c  = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);			// Return content, rather than printing it out
    	$c  = curl_setopt($ch, CURLOPT_NOBODY, 1);				    // Use HTTP "HEAD" METHOD
    	$c  = curl_setopt($ch, CURLOPT_HEADER, 1);					// Include response headers 
    	$x  = curl_exec($ch);										// Execute the request
    	$s	= curl_getinfo($ch, CURLINFO_HTTP_CODE); 				// Fetch the server response status
    	$c  = curl_close($ch);										// Close the cURL handle
    	return($s);													// Return the status to the caller
    }
    
    PHP:
    If you need the function to follow redirects (such as status code 301), then add this to the function before calling curl_exec():

    
    $c  = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
    PHP:
    Some example code using the function:

    
    $google_example1 = "http://www.google.com/fake";
    $google_example2 = "http://www.google.com/search?q=real";
    
    print("$google_example1: ".response_code($google_example1)."\r\n");
    print("$google_example2: ".response_code($google_example2)."\r\n");
    
    PHP:
    And the result:

    C:\>php example.php
    http://www.google.com/fake: 404
    http://www.google.com/search?q=real: 200
     
    papsyface, Nov 25, 2009 IP
  4. kbduvall

    kbduvall Peon

    Messages:
    71
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    0
    #4
    If the file is on the local machine use file_exists()

    Edit: "As of PHP 5.0.0, this function can also be used with some URL wrappers." so you could use that for a URL as well potentially.
     
    kbduvall, Nov 27, 2009 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    I didn't go over the code with a fine tooth comb, but papsyface is thinking the same thing I am with using cURL and a HEAD request.
     
    joebert, Nov 27, 2009 IP
  6. inanobot

    inanobot Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I didnt make a test but I think you can you file_get_contents()
    <?php
    $url = "http://asdf123456.com";
    if(!file_get_contents($url)) {
    echo "link is not exist";
    }
    else echo "yes it is exist";
    ?>
     
    Last edited: Nov 28, 2009
    inanobot, Nov 28, 2009 IP