Yogesh Chauhan's Blog

How can one check to see if a remote file exists using PHP?

in PHP on April 21, 2020

Use this Function


function remoteFileExists($url) {
    $curl = curl_init($url);

    //don't fetch the actual page, you only want to check the connection is ok
    curl_setopt($curl, CURLOPT_NOBODY, true);

    //do request
    $result = curl_exec($curl);

    $ret = false;

    //if request did not fail
    if ($result !== false) {
        //if request was ok, check response code
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  

        if ($statusCode == 200) {
            $ret = true;   
        }
    }

    curl_close($curl);

    return $ret;
}

$exists = remoteFileExists('http://stackoverflow.com/favicon.ico');
if ($exists) {
    echo 'file exists';
} else {
    echo 'file does not exist';   
}

Credit: Stack Overflow


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to remove trailing characters from a string using JavaScript?JavaScriptHow to create a placeholder loader (throbber) using CSS?CSSPostgreSQL transactions using the BEGIN, COMMIT, and ROLLBACK statements.PostgresMicroservices vs Monolithic ArchitectureMiscellaneousHow to convert a function component into a class in React?ReactHow to show slider value in HTML5 range input using JavaScript?HTMLHow to make a web page look good on any device?HTMLWhat does JSX do in React?ReactWhat is the difference between float and double?MiscellaneousAll possible ways you can Insert Data in PostgresPostgresOrder By and Group By in PostgresPostgresHow to animate list items using CSS and JavaScript?CSS