Jump to content

[SOLVED] Scanning directory.


waynew

Recommended Posts

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

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);
}

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).

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;
}

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

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?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.