0

I've get the file modification date for a local file with this code

<?php 
    $filename = 'db/ciupercomania.db';
    if (file_exists($filename)) {
        echo "This database was created on: <b>".date("F d Y H:i:s.", filemtime($filename)) . "</b><br/><br/>";
    } else {
        echo "File not exist.";
    }
?>

but I'm struggling to get it for a dropbox hosted file, where I use this code:

<?php
    $filename = 'https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db';
    if (file_exists($filename)) {
        echo "Out there is a database created on: <b>".date("F d Y H:i:s.", filemtime($filename)) . "</b><br/><br/>";
    } else {
        echo "File not exist.";
    }
?>

I want to mention that I tried filectime as well as the dropbox link with extension ?dl=0 and ?dl=1 (automatic or by choice download) without success.

For the dropbox link I always get the FILE NOT EXIST message. If I don't echo it, I get a warning: Warning: filemtime(): stat failed for https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db


Edit

Based on ADyson observation and his provided links, combined with other searches on this theme. I found out that filemtime() only works with files on the same server, and I should look for other solutions like curl_getinfo() or get_header(). Based on that I'm tried these codes:

<?php
  $url = 'https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db';
  $curl = curl_init($url);

  //don't fetch the actual page, i only want headers
  curl_setopt($curl, CURLOPT_NOBODY, true);

  //stop it from outputting stuff to stdout
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

  // attempt to retrieve the modification date
  curl_setopt($curl, CURLOPT_FILETIME, true);

// disabling SSL verification to a HTTPS website, I don't care about it.
//  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
//  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

  // disabling SSL verification for a HTTPS website, I don't care about it.
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

//Tell cURL where my certificate bundle is located.
//  $certificate = "cert/cacert.pem"; 
//  curl_setopt($curl, CURLOPT_CAINFO, $certificate);
//  curl_setopt($curl, CURLOPT_CAPATH, $certificate);

  //Check for errors.
  if(curl_errno($curl)){
    throw new Exception(curl_error($curl));
  }

   $result = curl_exec($curl);

//  if ($result === false) {
//   die (curl_error($curl));
//  }

  $timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
  if ($timestamp != -1) {  //otherwise unknown
    echo date("Y-m-d H:i:s", $timestamp); 
  } 
?>

and this one

<?php
  $url = 'https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db';
  $h = get_headers($url, 1);

  $dt = NULL;
  if(!$h || strpos($h[0], '200') !== false){
    $dt = new \DateTime($h['Last-Modified']);
    echo $dt;
  }
?>

First I've got SSL verification error and for that I tried both, ignoring it with CURLOPT_SSL_VERIFYHOST, false or CURLOPT_SSL_VERIFYHOST, 0, or by pointing at it with:

$certificate = "cert/cacert.pem"; 
curl_setopt($curl, CURLOPT_CAINFO, $certificate); 

but without any success. I don't receive errors but neither a result.

Returning at ADyson's observation and since I'm really dumb, I would really appreciate it, if someone can point me to where I'm doing wrong and help me out.

Szabolcs H
  • 39
  • 6
  • [file_exists](https://www.php.net/manual/en/function.file-exists.php) works on local or network filesystems. a URL is not a filesystem address. – ADyson Feb 16 '23 at 13:37
  • I'd suggest probably exploring the Dropbox API to see if it can return the information you're interested in, regarding a specific file. – ADyson Feb 16 '23 at 13:38
  • dropbox offers this information, but I don't know how to catch it – Szabolcs H Feb 16 '23 at 13:41
  • dropbox has a version history page where this info is present but also its present in the folder structure of the dropbox, itself – Szabolcs H Feb 16 '23 at 13:42
  • 2
    That's why you need to study the API documentation to see through which endpoints it might be exposed, and how to make requests to it. We can help you if you get stuck writing some PHP code to access the endpoint and retrieve the data, but it's not really our job to read through the API documentation for you. From a very quick google search it looks like it's probably held in the File Metadata (reference: https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Re-How-to-get-last-modified-date-for-folders-in-Dropbox-V2-API/td-p/391334) – ADyson Feb 16 '23 at 13:43
  • 1
    Probably worth reading https://help.dropbox.com/organize/modified-date for background, too. – ADyson Feb 16 '23 at 13:45
  • 2
    fyi, `$filename = 'https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db;` is missing a closing `'` – brombeer Feb 16 '23 at 13:46
  • 2
    Actually, since I was curious, and it was a 2-minute search, it's here: https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata . PHP SDKs are listed here: https://www.dropbox.com/developers/documentation/communitysdks . So you see...none of this is a secret, you just need to go and look for the info :-) – ADyson Feb 16 '23 at 13:47
  • I was there but these info quickly hide it from me :) thanks for your time – Szabolcs H Feb 16 '23 at 13:49
  • `these info quickly hide it from me`...I don't really know what you mean by that, but hopefully you are now on the right path to success. – ADyson Feb 16 '23 at 13:53
  • Thanks for the update. Is there any particular reason you tried this (probably unreliable) approach rather than using the proper API which will give you the proper information? I provieded you lots of links about how to achieve it, but it seems you just ignored all of that and tried to invent your own process, with no guarantee of success. – ADyson Feb 17 '23 at 12:49
  • yes, because without exageration, i'm dumb ( I'm not a IT programmer, but doing this for me as a hobby, and is more handy for me to alter a working code, instead of building a new one). I studied the links given by you, but I need to recognize that I'm lost in it. – Szabolcs H Feb 17 '23 at 13:06
  • Well, you are already making HTTP requests using cURL. Using the API just involves making different HTTP requests, essentially. However, you can use the PHP SDK if you like, to remove some of that complexity and allow you to run functions which should then call the API on your behalf, so that you don't have to build the exact cURL code yourself. – ADyson Feb 17 '23 at 13:12

0 Answers0