Page 1 of 1
How can I get the time that the file was last modified?
Posted: Sat Mar 03, 2012 5:07 pm
by APD1993
I am creating a series of scripts, one of which I would like to display the files in a particular directory. I think that I have managed to list the files through the use of a foreach loop, but I was wondering how I would be able to get the time that the file was last modified while using a foreach loop. However, I am getting an error message at the moment that reads:
Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\viewfile3.php on line 13, but I do not see what the problem is
The coding for my script is below:
Code: Select all
<html><head>
<title>RCM File List</title>
<link rel="stylesheet" type="text/css" a href="/rcm/stylesheet.css">
</head>
<body>
<h1>Files in RCM Directory</h1>
<?php
$dir="C:/xampp/htdocs/rcm/*txt";
$filename='C:/xampp/htdocs/rcm/denman2.txt';
if (file_exists($filename)) {
foreach(glob($dir) as $file)
{
$list = "<li><a href='readfile2.php'>Filename: $file</a></li><p>$filename was last changed on: " . date("F d Y H:i:s, filetime($filename));<br>";
}
}
?>
<ul>
<?php echo $list; ?>
</ul>
</body>
</html>
Any help is appreciated!

Re: How can I get the time that the file was last modified?
Posted: Sat Mar 03, 2012 6:05 pm
by litebearer
Try...
Code: Select all
$list = "<li><a href='readfile2.php'>Filename: " . $file , "</a></li><p>" . $filename . "was last changed on: " . date("F d Y H:i:s", filemtime($filename)) . "<br>";
Re: How can I get the time that the file was last modified?
Posted: Sun Mar 04, 2012 1:29 am
by APD1993
When I tried that, I got the same error messages as before

Re: How can I get the time that the file was last modified?
Posted: Sun Mar 04, 2012 2:46 am
by social_experiment
Code: Select all
$list = "<li><a href='readfile2.php'>Filename: $file</a></li><p>$filename was last changed on: " . date("F d Y H:i:s, filetime($filename));<br>";
// should be
$list = "<li><a href='readfile2.php'>Filename: $file</a></li><p>$filename was last changed on: " . date("F d Y H:i:s, filetime($filename))<br>";
You have an extra
; in the code above; the second sample should solve the problem
Re: How can I get the time that the file was last modified?
Posted: Sun Mar 04, 2012 3:23 am
by requinix
social_experiment wrote:the second sample should solve the problem
It won't. Yours is missing a
right after the date() call. Meanwhile litebearer's was right but for the typoed comma instead of a period.
Re: How can I get the time that the file was last modified?
Posted: Sun Mar 04, 2012 6:24 am
by litebearer
Sorry about the typo
ALSO the function is file
mtime NOT filetime.
Re: How can I get the time that the file was last modified?
Posted: Sun Mar 04, 2012 9:46 am
by APD1993
litebearer wrote:Try...
Code: Select all
$list = "<li><a href='readfile2.php'>Filename: " . $file , "</a></li><p>" . $filename . "was last changed on: " . date("F d Y H:i:s", filemtime($filename)) . "<br>";
That seems to have worked
I was just wondering if there was a way that I could subtract 1 hour from the time being displayed as it is saying that I modified the file last at 13:14 when it was at 12:14

Re: How can I get the time that the file was last modified?
Posted: Sun Mar 04, 2012 11:11 am
by litebearer
Code: Select all
date("F d Y H:i:s", filemtime($filename)-3600)
Re: How can I get the time that the file was last modified?
Posted: Sun Mar 04, 2012 12:59 pm
by requinix
If you're correcting for the "wrong" timezone then you might want other dates corrected too. Use
date_default_timezone_set.
Re: How can I get the time that the file was last modified?
Posted: Sun Mar 04, 2012 3:27 pm
by APD1993
Is there a way that I could implement this script so that if necessary, other text files in the RCM folder can be displayed? I added in a file called Test.txt and it is not being displayed in the script
Code: Select all
<!--Here, I am stating HTML coding with the <html> HTML tag. I am also creating a header and title (for the webpage) with the use of the <head> and <title> HTML tags. The title given to the webpage is RCM File List. I then am using the </title> and </head> HTML tags in order to stop the creation of the title and header of the webpage-->
<html><head>
<title>RCM File List</title>
<!--Here, I am creating a header and title (for the webpage) with the use of the <head> and <title> HTML tags. The title given to the webpage is Login Details. I then am using the </title> and </head> HTML tags in order to stop the creation of the title and header of the webpage-->
<link rel="stylesheet" type="text/css" a href="/rcm/stylesheet.css">
</head>
<!--Here, I am using the <body> HTML tag to create the body of the webpage so that content can be added to it-->
<body>
<!--Here, I am using the <h1> HTML tag to start creating a level 1 heading, and I am then using the </h1> HTML tag to stop creating the level 1 heading-->
<h1>Files in RCM Directory</h1>
<!--Here, I am starting a PHP script-->
<?php
//Here, I am creating a variable called dir and assigning it a value of a file path that helps act as a filter so that only files with the .txt extension such as the denman2.txt text file will be listed
$dir="C:/xampp/htdocs/rcm/*txt";
//Here, I am creating a variable called filename and I am assigning it a value of the filepath to the denman2.txt text file
$filename='C:/xampp/htdocs/rcm/denman2.txt';
//Here, I am creating an if statement branch stating that if the value assigned to the filename variable (i.e. the denman2.txt text file) exists, then go down this IF statement branch
if (file_exists($filename)) {
//Here, a foreach loop is being created while the files associated with the dir variable is being assigned as a variable called file
foreach(glob($dir) as $file)
{
//Here, I am creating a variable called list and I am assigning a formatted string that displays the name of the file associated with the file variable and then get the time that it was last modified so that it shows it in a Month day(number) year (number), hour:time:second format. I am also using "-3600" to set the time displayed back by an hour-->
$list = "<li><a href='readfile2.php'>Filename: " . $file . "</a></li><p>" . " Last Modified on: " . date("F d Y H:i:s", filemtime($filename)-3600) . "<br>";
}
}
//Here, I am ending the PHP script
?>
<!--Here, I am using the <ul> HTML tag to create an unordered list-->
<ul>
<!--I am starting a PHP script here-->
<!--I am using the echo keyword to write the value associated with the list variable to the webpage-->
<?php echo $list; ?>
<!--Here, I am using the </ul> HTML tag to end the creation of an unordered list-->
</ul>
<!--Here, I am ending the body and the adding of content to the webpage by using the </body> HTML tag and I am ending the HTML coding by using the </html> HTML tag-->
</body>
</html>
Thanks
