Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

chose newest file

windows os
scan a folder
chose the .xlxs file that is the newest
SOLUTION
Avatar of Slimshaneey
Slimshaneey
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Read this man page very carefully paying particular attention to the part about clearstatcache().
http://php.net/manual/en/function.filemtime.php

The highest value is attached to the newest file.
Avatar of rgb192

ASKER

// set this value to 1 for a random image, or zero for the latest image
$userand = 0;
// folder where the images are located
$dir = "targetfolder";
// filemask to match images
$filemask = "*.{gif}";
// name of the target image
$oldfile = "placeholderfile.gif";
//set a random seed using the system time
mt_srand( (double)microtime()*1000000 );
// change to the given directory
if ( $dir )
{
// change to the given directory
chdir( $dir );
// remove the old latest image - if it exists
if ( file_exists ( $dir . "/" . $oldfile ) ) { unlink( $dir . "/" . $oldfile ); }
// $files = glob( '*.{html,php,php4,txt}', GLOB_BRACE );
$files = glob( $filemask, GLOB_BRACE );
// count the number of files returned
$countfiles = count($files);
// only continue if files are found
if ( $countfiles > 0 )
{
// sort the files into date order
usort( $files, 'compare_filetime' );
// copy the selected file
if ( $userand == 0 )
{
// copy the file
copy ( $dir . "/" . $files[0] , $dir . "/" . $oldfile );
}
else
{
// select a random file
$randfile = mt_rand (0, $countfiles-1 );
// copy the random file
copy ( $dir . "/" . $files[$randfile] , $dir . "/" . $oldfile );
}
}
}
// compare the dates/times of files and return the latest one
function compare_filetime( $file1, $file2 )
{
return filemtime( $file2 ) - filemtime( $file1 );
}

Open in new window



I ran this file in a debugging php ide
and entered the function compare

but there was no output

what is the newest file





http://php.net/manual/en/function.filemtime.php

i dont understand how clearstatcache() can sort
how clearstatcache() can sort
It does not sort.  It clears the stat cache, so that you can get the newest, uncached information.

This code is full of conditional statements like if() and there is no indentation to indicate the control structures, so it's nearly impossible to read and understand the code.  Suggest you adopt a coding standard that indents the control structures 4 space each and lines up the curly braces so that the meaning and intent of the if() statements is clear to everyone.  

As to the part about "there was no output" all I can say is that you might want to consider using var_dump() to print out the contents of the variables.  In my experience you cannot over-use var_dump().  Some of the things I would want to see, if this were my application, include the $files variable after line 19 and again after line 26, and the return values from the copy() functions.  Once you see those variables you may be able to know what the programming is doing to the data sets.
Avatar of rgb192

ASKER

http://php.net/manual/en/function.filemtime.php#91665

there are still many if statements and the only output is the TIME and not the FILE
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

>>For obvious reasons I have disabled it on my server.  You might install it and try it on your server.  HTH, ~Ray

do I have to enable something in php.ini


your script found (what I think) is the oldest file

output:

for all files
stat failed for filename

including the last
stat failed for C:/Users/Acer/Documents/ppictures/picsZ Bath Feet.jpg

NEWEST IS C:/Users/Acer/Documents/ppictures/picsZ Bath Feet.jpg AT 1969-12-31T18:00:00-06:00
array(1) {
  ["1969-12-31T18:00:00-06:00"]=>
  string(53) "C:/Users/Acer/Documents/ppictures/picsZ Bath Feet.jpg"
}
I really meant it when I wrote this:
Read this man page very carefully paying particular attention to the part about clearstatcache().
http://php.net/manual/en/function.filemtime.php
This script works on Linux.  The date 1969-12-31 is symptomatic of trying to pass a non-numeric value (such as FALSE) to the date() function.  

You might want to check the link here:
http://php.net/manual/en/function.filemtime.php#100692
Avatar of rgb192

ASKER

for linux,
these solutions work

thanks