Search code examples
phpcsvcurllast-modified

Retrieve file last modified from file header using CURL


I was wondering if it's possible to acquire the 'last modified datetime' for a csv file using CURL.

I'm relying on a CSV file which is updated randomly to keep a database table up-to-date. I only want to update the database table with the CSV file data when the file has been modified after the last table update.

If you know of a link to docs that describe this I would be grateful if you could post them here.

I did some searching but my search engine was inundated with searches related to html headers.


Solution

  • You can do that with following way by using CURLOPT_HEADER

    <?php
    $curl = curl_init();
    
    curl_setopt($curl, CURLOPT_URL, $url);
    // Only header
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    // Do not print anything to output
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    // get last modified date
    curl_setopt($curl, CURLOPT_FILETIME, true);
    
    $result = curl_exec($curl);
    // Get info
    $info = curl_getinfo($curl);
    
    echo "Last modified time : " . date ("d-m-Y H:i:s", $info['filetime']) );
    ?>