mnevoso
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
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];
}
?>
ugh should be $filename=$parts[1]; for line 11.
ASKER
sorry my php is pretty basic what would the whole code look like
in a pinch and need it up before the am
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);
}
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.
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/";
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);
}
?>
ASKER
thank you i will try it.
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>Cli ck 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.
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>Cli
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.
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/joesim mel/html/r ates.php on line 9
Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/j/o/e/joesim mel/html/r ates.php on line 10
Newest file in /RateSheets/ is
i basically want to do a link that opens the latest file.
Warning: opendir(/RateSheets/) [function.opendir]: failed to open dir: No such file or directory in /home/content/j/o/e/joesim
Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/j/o/e/joesim
Newest file in /RateSheets/ is
i basically want to do a link that opens the latest file.
ASKER
the rates.php is the code you helped me create which queries the directory for the newest file and should open it
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/joesim mel/html/R ateSheets
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/joesim mel/html/r ates.php on line 10
Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/j/o/e/joesim mel/html/r ates.php on line 11
Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/o/e/joesim mel/html/r ates.php:1 0) in /home/content/j/o/e/joesim mel/html/r ates.php on line 6
/home/content/j/o/e/joesim
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/joesim
Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/j/o/e/joesim
Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/o/e/joesim
ASKER
actually figured that out but all gibberish comes up now
ASKER
nevermind looks like i got it to go
ASKER
you are awesome thank you so much
No problem, glad it does what you need!
ASKER
You the man!
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.
Open in new window