waynew Posted June 23, 2008 Share Posted June 23, 2008 Ok, my script selects information from database and writes it to a .doc file. What I'm trying to do next is scan through the directory that it was saved in, which I can do... but is there a way to choose the file that was last modified (ie last created) and then automatically start a download? Any help would be greatful! Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/ Share on other sites More sharing options...
lemmin Posted June 23, 2008 Share Posted June 23, 2008 This function will get the modification time of a file, but you will have to loop enumerate through all of the files in the folder and test them against eachother. http://www.php.net/manual/en/function.filemtime.php Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-572333 Share on other sites More sharing options...
waynew Posted June 23, 2008 Author Share Posted June 23, 2008 Okay, I have this so far: //scans given directory. function scan_directory($directory){ $i = 0; $filetimes = array(); //store filetime $files = array(); //store file $handle=opendir($directory); while (($file = readdir($handle))!==false) { $filetimes[$i] = filemtime($file); $files[$i] = $file; $i++; } closedir($handle); } Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-572338 Share on other sites More sharing options...
ober Posted June 23, 2008 Share Posted June 23, 2008 http://us2.php.net/asort Set the indexes of your array to the file name, then sort the array using the above function. Since your array values are simply integers, the biggest number will be the most recent file changed. Once it's sorted, I believe your last array element should be most recently modified file (and just grab the key of that one to get the file name). Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-572362 Share on other sites More sharing options...
lemmin Posted June 23, 2008 Share Posted June 23, 2008 Does it work? I don't know if it gives you access when you get the files that way. I would use something like this: function scan_directory($directory) { $files = glob($directory."\*"); $mostrecent = 0; foreach($files as $file) { $newtime = filemtime($file); if ($newtime > $mostrecent) { $mostrecent = $newtime; $newest = $file; } } return $newest; } Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-572365 Share on other sites More sharing options...
ober Posted June 23, 2008 Share Posted June 23, 2008 That would definitely be cleaner than my idea. Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-572371 Share on other sites More sharing options...
waynew Posted June 23, 2008 Author Share Posted June 23, 2008 Thanks guys. I reckon that I'm going the wrong way about it though. Is there a way to force the download of a .doc file? Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-572402 Share on other sites More sharing options...
lemmin Posted June 23, 2008 Share Posted June 23, 2008 This is from the header documentation on the PHP site: If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog. <?php // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('original.pdf'); ?> http://www.php.net/header Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-572480 Share on other sites More sharing options...
waynew Posted June 24, 2008 Author Share Posted June 24, 2008 Ok, what's happening is that I'm writing to a doc file in the same script. After writing to this file, I want it to start an automatic download. Now, after this file is written, I have the headers: header('Content-type: application/doc'); header('Content-Disposition: attachment; filename="downloaded.doc"'); readfile($file); //$file refers to the $file that was written. I keep getting this error however: Warning: readfile() expects parameter 1 to be string, resource given in C:\Apache\htdocs\losttenderletters.php on line 203 Surely it should be able to use the $file? Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-573060 Share on other sites More sharing options...
waynew Posted June 24, 2008 Author Share Posted June 24, 2008 Okay, I have it working. But it's not actually downloading. Instead its showing in the browser, on that very page. Is this because I'm on a localhost? Link to comment https://forums.phpfreaks.com/topic/111517-solved-scanning-directory/#findComment-573064 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.