Viewed   90 times

How do I check if a URL exists (not 404) in PHP?




$file = '';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
else {
    $exists = true;

From here and right below the above post, there's a curl solution:

function url_exists($url) {
    return curl_init($url) !== false;
Tuesday, September 20, 2022

The weird characters in the values passed in the URL should be escaped, using urlencode().

For example, the following portion of code :

echo urlencode('dsf13f3343f23/23=');

would give you :


Which works fine, as an URL parameter.

And if you want to build aquery string with several parameters, take a look at the http_build_query() function.

For example :

echo http_build_query(array(
    'id' => 'dsf13f3343f23/23=',
    'a' => 'plop',
    'b' => '$^@test', 

will give you :


This function deals with escaping and concatenating the parameters itself ;-)

Friday, October 14, 2022

The "domainNameSuffix" is called a top level domain (tld for short), and there is no easy way to extract it.

Every country has it's own tld, and some countries have opted to further subdivide their tld. And since the number of subdomains ( is also variable, there is no easy "one-regexp-fits-all".

As mentioned, you need a list. Fortunately for you there are lists publicly available:

Sunday, November 13, 2022

You could issue a "HEAD" request rather than a "GET"?

(edit) - lol! Looks like I've done this before!; changed to wiki to avoid accusations of rep-garnering. So to test a URL without the cost of downloading the content:

// using MyClient from linked post
using(var client = new MyClient()) {
    client.HeadOnly = true;
    // fine, no content downloaded
    string s1 = client.DownloadString("");
    // throws 404
    string s2 = client.DownloadString("");

You would try/catch around the DownloadString to check for errors; no error? It exists...

With C# 2.0 (VS2005):

private bool headOnly;
public bool HeadOnly {
    get {return headOnly;}
    set {headOnly = value;}


using(WebClient client = new MyClient())
    // code as before
Thursday, November 3, 2022

I used this, a bit easier:

// the server you wish to connect to - you can also use the server ip ex.
        $ftp_server = "";

// set up a connection to the server we chose or die and show an error
        $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// check if a file exist
        $path = "/SERVER_FOLDER/"; //the path where the file is located

        $file = "file.html"; //the file you are looking for

        $check_file_exist = $path.$file; //combine string for easy use

        $contents_on_server = ftp_nlist($conn_id, $path); //Returns an array of filenames from the specified directory on success or FALSE on error. 

// Test if file is in the ftp_nlist array
        if (in_array($check_file_exist, $contents_on_server)) 
            echo "<br>";
            echo "I found ".$check_file_exist." in directory : ".$path;
            echo "<br>";
            echo $check_file_exist." not found in directory : ".$path;  

        // output $contents_on_server, shows all the files it found, helps for debugging, you can use print_r() as well

// remember to always close your ftp connection

Functions used: (thanks to middaparka)

  1. Login using ftp_connect

  2. Get the remote file list via ftp_nlist

  3. Use in_array to see if the file was present in the array

Friday, September 16, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :