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
Check If a String Contains a Specific Word in PHPPHPWordPress 5.7 is migrating to latest jQuery version and people are freaking outjQueryHow to reprocess failed items in Etrieve Flow?MiscellaneousHow to set default timezone using PHP?PHPWhat is the difference between let and var in Swift?SwiftWhat are keys in React?ReactThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresCSS align-items property examplesCSSThe Sort and Compare Functions in JavaScriptJavaScriptSocial Media Colors: Sass VariablesMiscellaneousHow to create a Progress Bar using JavaScript?JavaScriptHow to add before after image effect using pure CSS?CSS