We help IT Professionals succeed at work.

Link to open newest file in drectory

mnevoso
mnevoso asked
on
I am trying to create a php file the querys a directory for a file in this format MM-DD-YY.xls and opens the newest file.

i have written some code, but does not seem to work can someone adjust it or point me in the right direction?

the folder that is holding the files is called RateSheets


<?php

$outfile = "RateSheets/".date("m-d-Y His").".xls";
$path = "RateSheets/";

echo "Most recent file is: ".getNewestFN($path);


function getNewestFN ($path) {

$p = opendir($path);
while (false !== ($file = readdir($p))) {
if (strstr($file,".xls"))
$list[]=date("mdYHis ", filemtime($path.$file)).$path.$file; 
}

rsort($list);

return $list[0];
}
?>

Open in new window

Comment
Watch Question

BRONZE EXPERT

Commented:
I'd do something like this for function getNewest();

Sorry if its a bit hokey, I slapped it together in a hurry because I have to go somewhere - if it doesn't work, I'll help you fix.
$handle=opendir($mydir);
while (false!==($filename=readdir($handle))) {
  if ( ($filename!="..")&&($filename!=".") ) {
    $stamp=filemtime("$mydir/$filename");
    array_push($files,"$stamp:$filename");
  }
}
sort($files);
$latest=$files[0];
$parts=explode(":",$latest);
$filename=$parts[0];
return($filename);

Open in new window

BRONZE EXPERT

Commented:
ugh should be $filename=$parts[1]; for line 11.

Author

Commented:
sorry my php is pretty basic what would the whole code look like
in a pinch and need it up before the am
BRONZE EXPERT

Commented:
Replace your function with mine.
function get_newest($directory) {
  $handle=opendir($mydir);
  while (false!==($filename=readdir($handle))) {
    if ( ($filename!="..")&&($filename!=".") ) {
      $stamp=filemtime("$mydir/$filename");
      array_push($files,"$stamp:$filename");
    }
  }
  sort($files);
  $latest=$files[0];
  $parts=explode(":",$latest);
  $filename=$parts[1];
  return($filename);
}

Open in new window

Author

Commented:
ok i will give this a try. on a side note i am linking to a php file that should open the latest xls correct.

Author

Commented:
and should this php file be in the same as the folder where the xls files are uploaded? and do i still need this code
$outfile = "RateSheets/".date("m-d-Y His").".xls";
$path = "RateSheets/";

Open in new window

BRONZE EXPERT

Commented:
I made a mess out of that.  Here is one that I just tested, and it works beautifully.
<?php

$directory="/path/to/somewhere";
$filename=get_newest($directory);
echo "Newest file in $directory is $filename\n";

function get_newest($directory) {
  $files=array();
  $handle=opendir($directory);
  while (false!==($filename=readdir($handle))) {
    if ( ($filename!="..")&&($filename!=".") ) {
      $stamp=filemtime("$directory/$filename");
      array_push($files,"$stamp:$filename");
    }
  }
  rsort($files);
  $latest=$files[0];
  $parts=explode(":",$latest);
  $filename=$parts[1];
  return($filename);
}

?>

Open in new window

Author

Commented:
thank you i will try it.

Author

Commented:
is this php file in the same folder as the uploaded files or a different location, i tried it no xls file pops up to open or save
BRONZE EXPERT

Commented:
No, this php file goes wherever you want in your web tree.  Your question never mentioned popping up any files to save - you just said you wanted to open it.

Ultimately, once you've identified the name of the newest file that you want to serve up the web site visitor, you have some options:

1)  If the file is already in your web root somewhere, you can just do a hyperlink:
echo "<a href=filedir/$filename>Click here to download $filename</a>\n";

2)  You can fopen() the file on the server and parse the binary data into a variable, then you can echo the appropriate headers to the browser which will prompt the user to save or open.  #1 above is easier, but if your file directory isn't visible in your web server root, the latter is what you'll have to do.  If that is the case, let me know and I'll give you the code.

Author

Commented:
i am getting this error when i go to www.afr-wholesale.com/rates.php

Warning: opendir(/RateSheets/) [function.opendir]: failed to open dir: No such file or directory in /home/content/j/o/e/joesimmel/html/rates.php on line 9

Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/j/o/e/joesimmel/html/rates.php on line 10
Newest file in /RateSheets/ is

i basically want to do a link that opens the latest file.

Author

Commented:
the rates.php is the code you helped me create which queries the directory for the newest file and should open it

Author

Commented:
so in a synopsis i would like to go to www.afr-wholesale.com/rates.php and the newwest file should open just as if i was going to www.afr-wholesale.com/ratesheets/name.xls
BRONZE EXPERT
Commented:
okay, that is easy - attached is rates.php, which will do exactly that.


<?php
# rates.php

$directory="/path/to/my/ratesheets";
$filename=get_newest($directory);
Header("Location:  http://www.afr-wholesale.com/ratesheets/$filename");

function get_newest($directory) {
  $files=array();
  $handle=opendir($directory);
  while (false!==($filename=readdir($handle))) {
    if ( ($filename!="..")&&($filename!=".") ) {
      $stamp=filemtime("$directory/$filename");
      array_push($files,"$stamp:$filename");
    }
  }
  rsort($files);
  $latest=$files[0];
  $parts=explode(":",$latest);
  $filename=$parts[1];
  return($filename);
}

?>

Open in new window

Author

Commented:
thanks, still getting this not sure why should't it just be /ratesheets ? or is it an absolute path?
/home/content/j/o/e/joesimmel/html/RateSheets
which i just tried and didnt work


Warning: opendir(/ratesheets) [function.opendir]: failed to open dir: No such file or directory in /home/content/j/o/e/joesimmel/html/rates.php on line 10

Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/j/o/e/joesimmel/html/rates.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/o/e/joesimmel/html/rates.php:10) in /home/content/j/o/e/joesimmel/html/rates.php on line 6

Author

Commented:
actually figured that out but all gibberish comes up now

Author

Commented:
nevermind looks like i got it to go

Author

Commented:
you are awesome thank you so much
BRONZE EXPERT

Commented:
No problem, glad it does what you need!

Author

Commented:
You the man!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.