YogeshChauhan . com

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

amazon

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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 14 How to add Google Map in WordPress using ACF? May 13 How to add a select dropdown in React? May 12 What are Controlled Components in React? May 11 How to create a full screen loader using CSS and JavaScript? May 11 How to hide and show menu on scroll using JavaScript and CSS? May 10 @use rule in SCSS (Sass)

You might also like these

JavaScript Number MethodsJavaScriptShort-Circuit Evaluation in JavaScriptJavaScriptSpecificity of attribute selectorsCSSWhat’s a repository on Github?MiscAccessing and Setting features of JavaScript ObjectsJavaScriptHow to create a Random Hex Color generator using JavaScript?JavaScriptLearn to make a responsive gallery using CSS Grid and without media queriesCSSHow to add a select dropdown in React?ReactStyling Lists with CSSCSSCSS Overflow Property with ExamplesCSSHow to get the first element with a class name xyz using JavaScript?JavaScriptWordPress: How to get ACF field values from another post?WordPress