|
 |
filemtime (PHP 3, PHP 4, PHP 5) filemtime -- Получить время последнего изменения файла Описаниеint filemtime ( string filename )
Функция возвращает время последнего изменения указанного файла
или FALSE в случае возникновения ошибки. Время
возвращается в формате 'Unix timestamp', который подходит
для передачи в качестве аргумента функции date().
Замечание: Результаты этой функции
кэшируются. Более подробную информацию смотрите в разделе
clearstatcache().
Подсказка: Начиная с
PHP 5.0.0, эта функция также может быть
использована с некоторыми упаковщиками url.
Список упаковщиков, поддерживаемых семейством функций
stat(), смотрите в Прил. M.
Данная функция возвращает время последней записи
блоков файла, иначе говоря, изменения содержания файла.
Пример 1. Пример использования функции filemtime()
<?php
$filename = 'somefile.txt';
if (file_exists($filename)) {
echo "в последний раз файл $filename был изменен: " . date ("F d Y H:i:s.", filemtime($filename));
}
?>
|
|
См.также описание функций filectime(), stat(),
touch() и getlastmod().
filemtime
madsen at lillesvin dot net
25-Sep-2005 03:39
There are a couple of things to point out about the otherwise great example posted by "notepad at codewalkers dot com".
First of all, as "dma05 at web dot de" pointed out, use HEAD instead of GET - that's being nice to your fellow man.
Second, it will only allow communication on port 80. That can easilly be solved.
<?php
function remote_filemtime($url)
{
$uri = parse_url($url);
$uri['port'] = isset($uri['port']) ? $uri['port'] : 80;
$handle = @fsockopen($uri['host'], $uri['port']);
}
?>
But hey, thanks a lot for the function! I've really had great use of it.
dma05 at web dot de
23-Apr-2005 02:25
concerning "notepad at codewalkers dot com"'s code:
this code is pretty neat, but i just wanted to note that using the "HEAD"-method instead of the "GET"-method in the http-request might be preferrable, since then not the whole resource is being downloaded...
http/1.1 definiton snippet:
Section "9.4 HEAD"
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification. [...]
-- snippet end ---
the code would then be...:
-- snippet ---
fputs($handle,"HEAD $uri[path] HTTP/1.1\r\nHost: $uri[host]\r\n\r\n");
-- snippet end ---
regards, Magnus
notepad at codewalkers dot com
11-Mar-2005 01:53
i needed the ability to grab the mod time of an image on a remote site. the following is the solution with the help of Joe Ferris.
<?php
function filemtime_remote($uri)
{
$uri = parse_url($uri);
$handle = @fsockopen($uri['host'],80);
if(!$handle)
return 0;
fputs($handle,"GET $uri[path] HTTP/1.1\r\nHost: $uri[host]\r\n\r\n");
$result = 0;
while(!feof($handle))
{
$line = fgets($handle,1024);
if(!trim($line))
break;
$col = strpos($line,':');
if($col !== false)
{
$header = trim(substr($line,0,$col));
$value = trim(substr($line,$col+1));
if(strtolower($header) == 'last-modified')
{
$result = strtotime($value);
break;
}
}
}
fclose($handle);
return $result;
}
?>
habazi at yahoo dot com
21-Feb-2005 10:13
"this is not (necessarily) correct, the modification time of a directory will be the time of the last file *creation* in a directory (and not in it's sub directories)."
This is not (necessarily) correct either. In *nix the timestamp can be independently set. For example the command "touch directory" updates the timestamp of a directory without file creation.
Also file removal will update the timestamp of a directory.
09-Dec-2004 08:30
A comment below states
"When using this function to get the modified date of a directory,
it returns the date of the file in that directory that was last modified."
this is not (necessarily) correct, the modification time of a directory will be the time of the last file *creation* in a directory (and not in it's sub directories).
adam at roomvoter dot com
01-May-2004 01:42
The snippet of code earlier that allows you to delete all files older than 2 weeks uses the function (filemtime) - which checks the original create date of the file (filesystem independent). You MAY want to use filectime() - that looks at when the file was last changed on YOUR file system.
if (is_dir("$path") )
{
$handle=opendir($path);
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$Diff = (time() - filectime("$path/$file"))/60/60/24;
if ($Diff > 14) unlink("$path/$file");
}
}
closedir($handle);
}
wookie at at no-way dot org
14-Sep-2003 02:17
Another little handy tool; to get the most recent modified time from files in a directory. It even does recursive directories if you set the $doRecursive param to true. Based on a file/directory list function I saw somewhere on this site. ;)
function mostRecentModifiedFileTime($dirName,$doRecursive) {
$d = dir($dirName);
$lastModified = 0;
while($entry = $d->read()) {
if ($entry != "." && $entry != "..") {
if (!is_dir($dirName."/".$entry)) {
$currentModified = filemtime($dirName."/".$entry);
} else if ($doRecursive && is_dir($dirName."/".$entry)) {
$currentModified = mostRecentModifiedFileTime($dirName."/".$entry,true);
}
if ($currentModified > $lastModified){
$lastModified = $currentModified;
}
}
}
$d->close();
return $lastModified;
}
paranoid at dds dot nl
05-Jun-2003 05:43
To get the last modification time of a directory, you can use this:
<pre>
$getLastModDir = filemtime("/path/to/directory/.");
</pre>
Take note on the last dot which is needed to see the directory as a file and to actually get a last modification date of it.
This comes in handy when you want just one 'last updated' message on the frontpage of your website and still taking all files of your website into account.
Regards,
Frank Keijzers
laurent dot pireyn at wanadoo dot be
27-Sep-2001 05:00
If you use filemtime with a symbolic link, you will get the modification time of the file actually linked to. To get informations about the link self, use lstat.
23-Aug-2001 07:08
When using this function to get the modified date of a directory, it returns the date of the file in that directory that was last modified.
jay at fudge dot org
29-Jun-1999 01:55
If you want this functionality for the parent web page you should use getlastmod()
i.e.
<?php echo "Last modified: ".date( "F d Y H:i:s.", getlastmod() ); ?>
within the included page... i.e. as a commont footer include for all pages
gerardj at home dot com
19-May-1999 08:27
The above code works fine if you place it on each page you want a date stamp on. I've found that if you place a reference such as filemtime(__FILE__) in an included or required file, that the modification time of the inherited file will be returned, not the time of the file that did the ineriting.
| |