How PHP obtains file size and file creation time _php tips

Source: Internet
Author: User

This example describes how PHP obtains the file size and file creation time. Share to everyone for your reference. The specific analysis is as follows:

PHP can display various properties of the file, including the file's last access time, last modified time, file size, and so on.

<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 is modified on";
Print Date ("L, M D, Y g:i a", $mtime);
print "<br>";
$ctime = Filectime ("Samplefile.doc");
Print "This file is changed on";
Print Date ("L, M D, Y g:i a", $ctime);
? >
</BODY>
</HTML>

Filemtime (string filename)

Returns the time when the file was last modified, FALSE when an error occurred. Time is returned as a Unix timestamp and can be used for date ().

Filectime (string filename)

Returns the time when the last inode was modified, or FALSE if an error occurs. Time is returned in the form of a Unix timestamp.

Fileatime (string filename)

Returns the time when the file was last accessed, or FALSE if an error occurs. Time is returned in the form of a Unix timestamp.

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

Filectime:linux Last Modification time
Filemtime: Last Modified time
Fileatime: Time of last visit

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

Filemtime
(PHP 3, PHP 4)

Filemtime--Get File modification time
Description

int Filemtime (string filename)

Returns the time when the file was last modified, FALSE when an error occurred. Time is returned as a Unix timestamp and can be used for date ().
Note: The result of this function will be cached. For more information, see Clearstatcache ().
Note: This function does not work on remote files, and the files that are checked must be accessed through the server's file system.
This function returns the time when the block of data in the file was last written, that is, when the contents of the file were last modified.

Example 1. Filemtime () example

<?php
//outputs e.g Somefile.txt was last Modified:december 2002.
$filename = ' somefile.txt ';
if (file_exists ($filename)) {
  echo "$filename is Last modified:". Date ("F D Y h:i:s.", Filemtime ($filename));
>

Filectime
(PHP 3, PHP 4)

Filectime--Gets the inode modification time of the file
Description

int Filectime (string filename)

Returns the time when the last inode was modified, or FALSE if an error occurs. Time is returned in the form of a Unix timestamp.
Note: In most Unix file systems, when the Inode data for a file is changed, the file is considered modified. That is, when the file's permissions, owner, all groups or other inode's metadata is updated. See Filemtime () (This is the function you want to use to create the "last updated" footnote in a Web page) and Fileatime ().
It is wrong to note that CTime is described in some Unix instruction text as the time the file was established. There is no time to establish UNIX files in most Unix file systems.
Note: The result of this function will be cached. For more information, see Clearstatcache ().
Note: This function does not work on remote files, and the files that are checked must be accessed through the server's file system.

Example 1. Fileatime () example

<?php

//output is similar: Somefile.txt was last Changed:december 2002 22:16:23.

$filename = ' somefile.txt ';
if (file_exists ($filename)) {
  echo "$filename is last changed:". Date ("F D Y h:i:s.", Filectime ($filename));
>

Fileatime
(PHP 3, PHP 4)

Fileatime-Gets the file's last access time
Description

int Fileatime (string filename)

Returns the time when the file was last accessed, or FALSE if an error occurs. Time is returned in the form of a Unix timestamp.
Note: The atime of a file should be changed whenever a block of data in this file is read. When an application accesses a large number of files or directories on a regular basis, performance is affected. Some Unix file systems can turn off atime updates at load time to improve the performance of such programs. USENET newsgroup spooling is a common example. This function is not useful in this file system.
Note: The result of this function will be cached. For more information, see Clearstatcache ().
Note: This function does not work on remote files, and the files that are checked must be accessed through the server's file system.
Example 1. Fileatime () example

<?php
//output is similar: Somefile.txt was last Accessed:december 2002 22:16:23.
$filename = ' somefile.txt ';
if (file_exists ($filename)) {
  echo "$filename is last accessed:". Date ("F D Y h:i:s.", Fileatime ($filename));
>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.