php获得文件大小和文件创建时间的方法


Posted in PHP onMarch 13, 2015

本文实例讲述了php获得文件大小和文件创建时间的方法。分享给大家供大家参考。具体分析如下:

php中可以显示文件的各种属性,这些属性包括文件的最后访问时间、最后修改时间、文件大小等。

<HTML>
<HEAD>
<TITLE>Returning information about a file</TITLE>
</HEAD>
<BODY>
<?php
print "The size of the file is ";
print filesize( "samplefile.doc" );
print "<br>";
$atime = fileatime( "samplefile.doc" );
print "This file accessed on ";
print date("l, M d, Y g:i a", $atime);
print "<br>";
$mtime = filemtime( "samplefile.doc" );
print "This file was modified on ";
print date("l, M d, Y g:i a", $mtime);
print "<br>";
$ctime = filectime( "samplefile.doc" );
print "This file was changed on ";
print date("l, M d, Y g:i a", $ctime);
?>
</BODY>
</HTML>

filemtime ( string filename )

返回文件上次被修改的时间,出错时返回 FALSE。时间以 Unix 时间戳的方式返回,可用于 date()。

filectime ( string filename )

返回文件上次 inode 被修改的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。

fileatime ( string filename )

返回文件上次被访问的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。

////////////////////////////

filectime:linux最后一次修改时间
filemtime:最后一次修改时间
fileatime:最后一次访问的时间

/////////////////////////////////////////////////////////////////////////////

filemtime
(PHP 3, PHP 4 )

filemtime -- 取得文件修改时间
说明

int filemtime ( string filename)

返回文件上次被修改的时间,出错时返回 FALSE。时间以 Unix 时间戳的方式返回,可用于 date()。
注: 本函数的结果会被缓存。详细信息参见 clearstatcache()。
注: 本函数不能作用于远程文件,被检查的文件必须通过服务器的文件系统访问。
本函数返回文件中的数据块上次被写入的时间,也就是说,文件的内容上次被修改的时间。

例子 1. filemtime() 例子

<?php
// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.
$filename = 'somefile.txt';
if (file_exists($filename)) {
  echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));

}
?>

filectime
(PHP 3, PHP 4 )

filectime -- 取得文件的 inode 修改时间
说明

int filectime ( string filename)

返回文件上次 inode 被修改的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。
注意:在大多数 Unix 文件系统中,当一个文件的 inode 数据被改变时则该文件被认为是修改了。也就是说,当文件的权限,所有者,所有组或其它 inode 中的元数据被更新时。参见 filemtime()(这才是你想用于在 web 页面中建立“最后更新时间”脚注的函数)和 fileatime()。
注意某些 Unix 说明文本中把 ctime 说成是该文件建立的时间,这是错的。在大多数 Unix 文件系统中没有 Unix 文件的建立时间。
注: 本函数的结果会被缓存。详细信息参见 clearstatcache()。
注: 本函数不能作用于远程文件,被检查的文件必须通过服务器的文件系统访问。

例子 1. fileatime() 例子

<?php

// 输出类似:somefile.txt was last changed: December 29 2002 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
  echo "$filename was last changed: " . date ("F d Y H:i:s.", filectime($filename));
}
?>

fileatime
(PHP 3, PHP 4 )

fileatime -- 取得文件的上次访问时间
说明

int fileatime ( string filename)

返回文件上次被访问的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。
注意:一个文件的 atime 应该在不论何时读取了此文件中的数据块时被更改。当一个应用程序定期访问大量文件或目录时很影响性能。有些 Unix 文件系统可以在加载时关闭 atime 的更新以提高这类程序的性能。USENET 新闻组假脱机是一个常见的例子。在这种文件系统下本函数没有用处。
注: 本函数的结果会被缓存。详细信息参见 clearstatcache()。
注: 本函数不能作用于远程文件,被检查的文件必须通过服务器的文件系统访问。
例子 1. fileatime() 例子

<?php
// 输出类似:somefile.txt was last accessed: December 29 2002 22:16:23.
$filename = 'somefile.txt';
if (file_exists($filename)) {
  echo "$filename was last accessed: " . date ("F d Y H:i:s.", fileatime($filename));
}
?>

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
php at(@)符号的用法简介
Jul 11 PHP
Codeigniter实现处理用户登录验证后的URL跳转
Jun 12 PHP
PHP中使用Imagick实现各种图片效果实例
Jan 21 PHP
支持png透明图片的php生成缩略图类分享
Feb 08 PHP
php基础设计模式大全(注册树模式、工厂模式、单列模式)
Aug 31 PHP
smarty学习笔记之常见代码段用法总结
Mar 19 PHP
php metaphone()函数的定义和用法
May 15 PHP
php图形jpgraph操作实例分析
Feb 22 PHP
PHP 实现公历日期与农历日期的互转换
Sep 13 PHP
php无限级分类实现评论及回复功能
Feb 18 PHP
Yii2.0框架behaviors方法使用实例分析
Sep 30 PHP
PHP代码加密的方法总结
Mar 13 PHP
php查看网页源代码的方法
Mar 13 #PHP
php中通过DirectoryIterator删除整个目录的方法
Mar 13 #PHP
php遍历删除整个目录及文件的方法
Mar 13 #PHP
php计算指定目录下文件占用空间的方法
Mar 13 #PHP
php中将一个对象保存到Session中的方法
Mar 13 #PHP
php实现httpRequest的方法
Mar 13 #PHP
php使用curl简单抓取远程url的方法
Mar 13 #PHP
You might like
example2.php
2006/10/09 PHP
discuz authcode 经典php加密解密函数解析
2020/07/12 PHP
详解PHP执行定时任务的实现思路
2015/12/21 PHP
PHP读取文件内容的五种方式
2015/12/28 PHP
PHP实现linux命令tail -f
2016/02/22 PHP
PHP弱类型语言中类型判断操作实例详解
2017/08/10 PHP
THINKPHP5分页数据对象处理过程解析
2020/10/28 PHP
JS判断页面加载状态以及添加遮罩和缓冲动画的代码
2012/10/11 Javascript
js跳转页面方法实现汇总
2014/02/11 Javascript
jQuery原型属性和原型方法详解
2015/07/07 Javascript
jquery实现TAB选项卡鼠标经过带延迟效果的方法
2015/07/27 Javascript
jQuery绑定事件的几种实现方式
2016/05/09 Javascript
vue中的计算属性的使用和vue实例的方法示例
2017/12/04 Javascript
微信小程序实现手势图案锁屏功能
2018/01/30 Javascript
Vue2 轮播图slide组件实例代码
2018/05/31 Javascript
JavaScript使用math.js进行精确计算操作示例
2018/06/19 Javascript
jQuery ajax仿Google自动提示SearchSuggess功能示例
2019/03/28 jQuery
解决vue v-for src 图片路径问题 404
2019/11/12 Javascript
python的Template使用指南
2014/09/11 Python
python3读取csv和xlsx文件的实例
2018/06/22 Python
pandas筛选某列出现编码错误的解决方法
2018/11/07 Python
Python实现统计英文文章词频的方法分析
2019/01/28 Python
Python玩转Excel的读写改实例
2019/02/22 Python
pandas DataFrame行或列的删除方法的实现示例
2019/08/02 Python
Python函数式编程指南:对生成器全面讲解
2019/11/19 Python
python 定义类时,实现内部方法的互相调用
2019/12/25 Python
详解pandas绘制矩阵散点图(scatter_matrix)的方法
2020/04/23 Python
python 实现socket服务端并发的四种方式
2020/12/14 Python
SneakerStudio英国:最佳运动鞋商店
2019/05/22 全球购物
求职信内容考虑哪几点
2013/10/05 职场文书
一份没有按时交货失信于客户的检讨书
2014/09/19 职场文书
专业见习报告范文
2014/11/03 职场文书
2014年药房工作总结
2014/11/22 职场文书
2015年采购员工作总结
2015/04/27 职场文书
运动会100米广播稿
2015/08/19 职场文书
JavaScript中MutationObServer监听DOM元素详情
2021/11/27 Javascript