filemtime

  • peter
  • Newbie
  • Newbie
  • User avatar
  • Posts: 7
  • Loc: Netherlands

Post 3+ Months Ago

Question?

Is there a way to read the creatingtime of a file from an other site or host in php or javascript?
I made this script, but it doesn't work. It only works with the absolute pad :cry:

<?php
$msgbestand = 'http://www.webfreakies.nl/chatbox/msg.txt';
$filemod = filemtime($msgbestand);
$filemodtimex = date("U", $filemod);
echo $filemodtimex;
?>
Who know's more? :idea: Already thanks. :D
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

I think you'll only be able to get this info if the file is chmodded **7 so public can access it.

Not sure though.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

If you check the PHP manual you'll see that it says you can't use filemtime on remote files, but you could try something like this to see if the server returns the date modified in the header.

Code: [ Select ]
<?
$domain = "www.webfreakies.nl";
$file = "/chatbox/msg.txt";
$fp = fsockopen ($domain, 80, $errno, $errstr, 30);
$header = "HEAD $file HTTP/1.0\r\n\r\n";

if (!$fp) {
    echo "$errstr ($errno)";
}
else {
    fputs ($fp, "$header");
    while (!feof($fp)) {
        $output .= fgets ($fp,128);
    }
    fclose ($fp);
}

$subs = explode("\n", $output);
$counter=0;
$lastModified = "No Date Modified Found";

while($counter < count($subs)) {
    if(substr_count($subs[$counter], "Modified")) {
        $lastModified = $subs[$counter];
        $lastModified = substr($lastModified, 15, strpos($lastModified,"\n")-1);
    }
    $counter++;
}

echo $lastModified;
echo "\n<br>\n";
echo $output;
?>
  1. <?
  2. $domain = "www.webfreakies.nl";
  3. $file = "/chatbox/msg.txt";
  4. $fp = fsockopen ($domain, 80, $errno, $errstr, 30);
  5. $header = "HEAD $file HTTP/1.0\r\n\r\n";
  6. if (!$fp) {
  7.     echo "$errstr ($errno)";
  8. }
  9. else {
  10.     fputs ($fp, "$header");
  11.     while (!feof($fp)) {
  12.         $output .= fgets ($fp,128);
  13.     }
  14.     fclose ($fp);
  15. }
  16. $subs = explode("\n", $output);
  17. $counter=0;
  18. $lastModified = "No Date Modified Found";
  19. while($counter < count($subs)) {
  20.     if(substr_count($subs[$counter], "Modified")) {
  21.         $lastModified = $subs[$counter];
  22.         $lastModified = substr($lastModified, 15, strpos($lastModified,"\n")-1);
  23.     }
  24.     $counter++;
  25. }
  26. echo $lastModified;
  27. echo "\n<br>\n";
  28. echo $output;
  29. ?>


I tried this on the file that you gave but got a 404, so it showed this:

Code: [ Select ]
No Date Modified Found
<br>
HTTP/1.1 404 Not Found
Date: Sat, 13 Dec 2003 01:46:45 GMT
Server: Apache/1.3.26 (Unix) Debian GNU/Linux mod_ssl/2.8.9 OpenSSL/0.9.6g mod_gzip/1.3.19.1a PHP/4.1.2
X-Powered-By: PHP/4.1.2
Connection: close
Content-Type: text/html; charset=iso-8859-1
X-Pad: avoid browser bug
  1. No Date Modified Found
  2. <br>
  3. HTTP/1.1 404 Not Found
  4. Date: Sat, 13 Dec 2003 01:46:45 GMT
  5. Server: Apache/1.3.26 (Unix) Debian GNU/Linux mod_ssl/2.8.9 OpenSSL/0.9.6g mod_gzip/1.3.19.1a PHP/4.1.2
  6. X-Powered-By: PHP/4.1.2
  7. Connection: close
  8. Content-Type: text/html; charset=iso-8859-1
  9. X-Pad: avoid browser bug


But when I tried it on one of my own files on a remote server it worked:

Code: [ Select ]
Thu, 17 Jul 2003 23:43:53 GMT
<br>
HTTP/1.1 200 OK
Date: Sat, 13 Dec 2003 01:51:43 GMT
Server: Apache/1.3.27 (Unix) mod_log_bytes/0.3 FrontPage/5.0.2.2510 PHP/4.3.2 mod_ssl/2.8.14 OpenSSL/0.9.7c
Last-Modified: Thu, 17 Jul 2003 23:43:53 GMT
ETag: "41bdb-23c-3f1734b9"
Accept-Ranges: bytes
Content-Length: 572
Connection: close
Content-Type: text/html
  1. Thu, 17 Jul 2003 23:43:53 GMT
  2. <br>
  3. HTTP/1.1 200 OK
  4. Date: Sat, 13 Dec 2003 01:51:43 GMT
  5. Server: Apache/1.3.27 (Unix) mod_log_bytes/0.3 FrontPage/5.0.2.2510 PHP/4.3.2 mod_ssl/2.8.14 OpenSSL/0.9.7c
  6. Last-Modified: Thu, 17 Jul 2003 23:43:53 GMT
  7. ETag: "41bdb-23c-3f1734b9"
  8. Accept-Ranges: bytes
  9. Content-Length: 572
  10. Connection: close
  11. Content-Type: text/html

I echoed the $output variable just to see the whole header, and you would probably want to convert the $lastModified into date format.

I threw this together rather quickly based on some examples that I've seen in the past, so I suggest it merely as an example of how to go about it in a general sense rather than a specific solution.
  • peter
  • Newbie
  • Newbie
  • User avatar
  • Posts: 7
  • Loc: Netherlands

Post 3+ Months Ago

Thank you for the answers.
I trieded the script. But it don't work for me. I trieded it also on an .jpg and .gif on three differed servers. But no date of the file.

I need the file to check if the file is modificate while I use is for the chatbox on http://www.webfreakies.nl . I like to run the refresh on an differend server while it take's to much cpu power. The chatbox is in frames and 1 frame is refreshing. An script checked if the msg.txt file is modificate, if so the the main frame wil be refreshed.
But if this can't be done, I put the box totaly on the other server but I then have to solve other problems.

But I still think it must be possible to get the date of that file from an remote server. If you have a picture and you rightclick, then properties, then there is the date.
Why can't it be done in php? :shock:

greetings Peter. :)
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Yeah it seems to depend on how the server is configured. I'm getting results for one of my domains, but not for the other. I tried it on a bunch of other servers, and the results were disappointing. Some returned the date modified in the header, but a majority did not.

When I right-click the pictures and pages of various sites I get either a "Not Available" or the current date, so I'm not sure that's an indication that the information is always available to the browser, but I'm going to keep trying to see if I can come up with something.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 110 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.