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;   


    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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to add a Line Chart in Angular App?AngularWhat is PostgreSQL? How similar or different it is from SQL?Postgresthe box-sizing property in CSSCSSHow to make WordPress main stylesheet (style.css)?WordPressUNION and UNION ALL in PostgresPostgresHow to use GROUPING SETS to boost GROUP BY queries in Postgres?PostgresHow to show slider value in HTML5 range input using JavaScript?HTMLFive common features of Angular template syntax (with examples)AngularThe ALPHA function in Envision BasicEnvision BasicHow to Remove PHP File Extensions From Your Website URLs?PHPHow to convert an object from API to JSON array in Angular 9?AngularWhy it’s not a good idea to create Number objects in JavaScript?JavaScript