Link to home
Start Free TrialLog in
Avatar of mnevoso
mnevosoFlag for United States of America

asked on

Link to open newest file in drectory

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

Avatar of xterm
xterm

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

ugh should be $filename=$parts[1]; for line 11.
Avatar of mnevoso

ASKER

sorry my php is pretty basic what would the whole code look like
in a pinch and need it up before the am
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

Avatar of mnevoso

ASKER

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.
Avatar of mnevoso

ASKER

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

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

Avatar of mnevoso

ASKER

thank you i will try it.
Avatar of mnevoso

ASKER

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
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.
Avatar of mnevoso

ASKER

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.
Avatar of mnevoso

ASKER

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

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of xterm
xterm

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 mnevoso

ASKER

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
Avatar of mnevoso

ASKER

actually figured that out but all gibberish comes up now
Avatar of mnevoso

ASKER

nevermind looks like i got it to go
Avatar of mnevoso

ASKER

you are awesome thank you so much
No problem, glad it does what you need!
Avatar of mnevoso

ASKER

You the man!