Home php教程 php手册 php如何获取文件大小和文件创建时间

php如何获取文件大小和文件创建时间

Jun 06, 2016 pm 08:06 PM
php

这篇文章主要介绍了php获得文件大小和文件创建时间的方法,涉及php中filesize及fileatime函数的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<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>

Copy after login

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() 例子

1

2

3

4

5

6

7

8

<?php

// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.

$filename = &#39;somefile.txt&#39;;

if (file_exists($filename)) {

  echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));

 

}

?>

Copy after login

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() 例子

1

2

3

4

5

6

7

8

9

<?php

 

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

 

$filename = &#39;somefile.txt&#39;;

if (file_exists($filename)) {

  echo "$filename was last changed: " . date ("F d Y H:i:s.", filectime($filename));

}

?>

Copy after login

fileatime
(PHP 3, PHP 4 )

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

int fileatime ( string filename)

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

1

2

3

4

5

6

7

<?php

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

$filename = &#39;somefile.txt&#39;;

if (file_exists($filename)) {

  echo "$filename was last accessed: " . date ("F d Y H:i:s.", fileatime($filename));

}

?>

Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles