Checking files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Checking files

Post by fernado1283 »

Hi,

I wondering anyone can help me with this please.
I had a drop down menu list which displays all the files in the folder.
Can anyone please tell me, how am I going to display only the lastest 10 files in the drop down menu. e.g I have 25 files in the folder, but I only want the lastest 10 files to be in the drop down menu for users to download.
Can anyone out there please help me. Thanks. :)

Code: Select all

<?php
if (isset($actionflag) && $actionflag == "download")
	{
		$passing = $selectXYZ;
	}
	htmlheader(); // prints html header


	$dir = "./"; // current directory


	// Print the directory name as a heading title 
	$filedir =  getcwd();  // get current dir
	$dirname = getHeader($filedir);
	print "<br>";
	print "<font size=+1 face="Times New Roman" color="Blue"><b> ";
	print "$dirname </b>";
	print "</font> ";
	print "<hr>";
	
	
	
	$file_array = array();
	$file_array = getFiles($dir); // displays all the files in the folder

	print "<form action="openFiles.php " name="selection" target="_blank">";
	print "<input type="hidden" name="actionflag" value="download">";
	print "<select name="selectXYZ">";

	
	foreach($file_array as $a_row)
	{
		// make sure it's not an include file or index file
		if (!strstr($a_row,".inc.php") && !strstr($a_row,"index.php") && !strstr($a_row, ".php"))
		{
			if (!is_dir($a_row)) // the file is not a directory
			{            	
				print "<option>$a_row</option>";
			}				
		}
	}

	print "</select> ";

	print "<input type="submit">";
	print "</form>";
?>
Regards...
Fernado
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Change the

Code: Select all

foreach($file_array as $a_row)
to

Code: Select all

for ($i=0;$i=10;$i++)
// and change the $a_row to match etc etc
...then you have a loop that will only print 10 files. You should sort the files according to dates in your getFiles() function to have them in the order you want.
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post by fernado1283 »

Hi,
I've made the changes but it just wont display anything???
Can anyone help me please. Thanks...

Code: Select all

<?php
for ($i=0; $i=10; $i++)
	{
		$a_row = array();
		$a_row = $file_array(i); 
	
		// make sure it's not an include file or index file
		if (!strstr($a_row,".inc.php") && !strstr($a_row,"index.php") && !strstr($a_row, ".php"))
		{
			if (!is_dir($a_row)) // the file is not a directory
			{            	
				print "<option>$a_row</option>";
			}				
		}
	}
?>
Regards...
Fernado
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

You should sort the files according to dates in your getFiles() function to have them in the order you want.
How did that turn out?
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post by fernado1283 »

Hi,

What I mean initially was that after i change to this code, not a single file are being added into the drop down menu. The drop down menu become empty.
Do you know whats wrong with it.

Regards...
Fernado
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

as getFiles is a user function we have to guess at the array returned - you'll probably need to amend that function anyway to include the filemtime of each file.

either post the function getFiles() here or a var_dump or print_r of the found $file_array. As you are getting nowt I'd guess that the index holds the filename and the value is another array - that'd just be a guess though and may confuse further - so best to determine properly the function return.
Post Reply