I wrote a CMS script made of many folders and files and I want to find a way to track when I last modified any of the files. I wrote a recursive directory/file check that finds the latest modified file and gives me the date and time however my issue is this: every time that I as much as copy a file to the server, or rename a file, even if I didn't make any modifications at all to any of the files, the newly copied file or renamed file now has today's date and therefore my script shows that there was a modification made today even if I haven't made changes in weeks.
How can I circumvent that?
I am using filemtime()
Is there a way with PHP to know when the file was ACTUALLY last modified (ie when the code in a file was worked on the last time)?
Thanks
I found a way to do it and wanted to post the answer:
$test = new SplFileInfo('path/to/file');
echo $test->getMTime();
echo date('Y-m-d',$test->getMTime());
The SplFileInfo::getMTime will actually return the last time a file's contents were modified as opposed to the last modification date of the file
Related
I am facing a peculiar problem in parsing an excel file (.xls) using PHPExcelReader which actually Spreadsheet_Excel_Reader class. I have used it so many times in different applications and every time I was fine. I am working for an app where there is a ftp folder and an excel file is remotely put there every day. I have a scheduler task that runs every day and read the excel file and update the database.
It was working very fine for couple of months. Now they are adding some new columns in the files and the Spreadsheet_Excel_Reader is unable to read the numeric and date values. But if I just open the file and hit CTRL+S without doing anything, svn says that the file has been modified although I don't see anything changed from 'SVN: Diff with previous version'. However it is doing the magic as I see that the saved file is parsed correctly.
Bellow is the result I see when I try to run the script without touching the file. Please look at index 5 to 9.
Now look at the parse result when I run the script after opening the file and hit CTRL+S. Now entirely sure what is happening. I contacted to them and they said they are not doing anything new.
Any idea about this problem?
Sharing the idea here is much appreciated.
How are you looping through and grabbing the cell values? Have you tried using $cell->val(12,6)? An alternative could be (Thanks to Mark Baker):
$range = 'F'.$row.':'.'J'.$row; # assuming F-J cols are your numeric cols
$objPHPExcel->getActiveSheet()
->getStyle($range)
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
In my WordPress site, I have a script that looks at a directory and uploads each image in that directory to a specific post. Right now, this is set to happen each time the user opens the post edit page. I need a way to check if the contents of the directory have changed. That way, I can set the script to not run if the contents of the directory have not changed. Is there a way to accomplish this in PHP?
I would try to make a hash of directory content, let's say concating the names of files (sorted) with some divider and making md5/sha1 hash. You'll need to store it in some way (in directory as text file or database).
On access you'll have to calculate actual hash of directory in the same way and compare it with the old (saved) one. Depending on result, you can take some actions...
You can check the modification date of a file with filetime
http://php.net/manual/de/function.filemtime.php
A solution is to store the day of the last visit and look for younger files at the next visit.
You can open every file in PHP and generate checksums with MD5, then save this with time() to text file in directory. Next time you can compare yours checksums and you will know if anthing has changed since last time.
So I'm trying to see if something like this is possible WITHOUT using database.
A file is uploaded to the server /files/file1.html
PHP is tracking the upload time by checking last update time in database
If the file (file1.html) has been updated since the last DB time, PHP makes changes; Otherwise, no changes are made
Basically, for a text simulation game (basketball), it outputs HTML files for rosters/stats/standings/etc. and I'd like to be able to insert each team's Logo at the top (which the outputted files don't do). Obviously, it would need to be done often as the outputted files are uploaded to the server daily. I don't want to have to go through each team's roster manually inserting images at the top.
Don't have an example as the league hasn't started.
I've been thinking of just creating a button on the league's website (not created yet) that when pushed would update the pages, but I'm hoping to have PHP do it by itself.
Yes, you could simply let php check for the file creation date (the point in time where the file was created on the server, not the picture itself was made). check http://php.net/manual/en/function.filemtime.php and you should be done within 30mins ;)
sexy quick & dirty unproven code:
$filename = 'somefile.txt';
$timestamp_now = time(); // get timestamp from now (seconds)
if (filemtime($filename) > $timestamp_now) {
// overwrite the file (maybe check for existing file etc first)
}
I will be sending new files over from one computer to another computer. How do I make PHP auto detect new/updated files in the folders and enter the information inside the files into mysql database?
Get all files you already know from the database
loop through the directory with http://www.php.net/manual/de/function.readdir.php
if the file is known, do nothing
if the file is not known, add it to the database
In the end, delete all files no longer in the directory
I would pick a set-up where new files and old fields are in a separate directory.
But if you have no choice, you could check the modification date and match it with your last directory iteration. (Use filemtime for this).
Don't forget to do some database checking when you process an image though.
Save the timestamp of the last check and when you check next look at the fileinfo and check creation date. Even better yet because you store filecontens in a database, check for the time it was modified using: filemtime()
You can't. PHP works as a preprocessor and even it has execution time limit (set in the configuration). If you need to process with PHP then make a PHP script that outputs a web page that use meta redirection to itself. Inside the script, you should loop over the files, query the database for the file name and its modification time, if it exists then nothing to do, otherwise, if the file name exists then it's an update, otherwise it's a new file.
I'm trying to write a cronjob in php to search a server for newly uploaded files. Every night the server adds a csv file, which must be pulled to my local server, and inserted into my database. I can read the csv file, insert it into the database, and everything else on my end, except figure out how to scan the directory for the new file every night. Does anybody have any general suggestions for going about this?
Algorithm:
Scan the directory, order files by date, and record the date of the most recent file
On subsequent scans, compare the date of the newest file with the recorded date
If the date is newer, a new file has been uploaded.
While you can technically do it with bare PHP, I'd go for the find command:
$files = explode("\0",`find /path/to/dir -mtime -1 -iname '*.csv' -print0`);
Using scandir, make an array $allFiles that contains all the files. Create another array $oldFiles containing all the existing files in the directory. After that, performing array_diff($allFiles, oldFiles) will yield an array containing only the new files.