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

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to select multiple values in React dropdown?ReactHow to check if the element exists using JavaScript and jQuery?JavaScriptHow can one check to see if a remote file exists using PHP?PHPHow to add a select dropdown in React?React3 Types of Arrays in PHPPHPINTERSECT and EXCEPT in PostgresPostgresHow services and dependency injection work in Angular?AngularPostgreSQL BETWEENPostgresHow to add a Pie Chart in Angular App?AngularHow Routing works in Angular?AngularLEFT JOIN in PostgresPostgresLearn how to add Scroll Indicator using CSS and JavaScript?CSS