Page 1 of 1

Parsing the files into an array for downloading

Posted: Wed Aug 27, 2003 9:49 pm
by fernado1283
Hi,
I need help for my php script. I have a folder with many jpeg files. Initially, I do it and display them in table for users to click and view. But now, I need them to be display in a drop down menu and when user click on it, they are able to download the file. Can anyone please help me how to do it. Thanks.
My code is as below for your reference.

Code: Select all

<?php
function getFiles($dir)
{
	$filedir =  getcwd();
	$filename = getHeader($filedir);
	

		print "<table border=1 align=center cellpadding=4 width=725>";
		print "<tr>
				<th align=left bgcolor="yellow" width=500><font size=-1 face="Times New Roman"><b>Name:</b></font></th>
				<th align=left bgcolor="yellow" width=125><font size=-1 face="Times New Roman"><b>Size:</b></font></th>
				<th align=left bgcolor="yellow" width=100><font size=-1 FACE="Times New Roman"><b>Updated:</b></font></th>
	  		   </tr>";
	
	
	if (is_dir($dir)) // check if it's a valid directory
	{
		
		
		$dir = @opendir($dir); // open dir
	
		if($dir) // if it IS a dir
		{
			while (($file = @readdir($dir)) == true) // read from the dir get filenames
			{
				if ($file != "." && $file != ".." && $file != ".DS_Store" && $file !="drop box") //don't display certain filenames
				{ 
					$file_array[]=$file; //put into array
				
				}
			}
		}
	
		
		sort($file_array);
		reset($file_array);
		
				
		// printing	
		for($i=0;$i<count($file_array);$i++) // loop the array
		{
			$nfile = $file_array[$i]; // get each row
			
			// make sure it's not an include file or index file
			if (!strstr($nfile,".inc.php") && !strstr($nfile,"index.php") && !strstr($nfile, ".php"))
			{
				
				$fsize=round(filesize($nfile)/1000)*1; //get filesize
				$datemod = date("m/d/y", filemtime($nfile)); // get filedate
								
				if (is_dir($nfile)) // the file is a directory
				{ 
				}
				
				else // the file is not a directory
				{              
					print "<tr><td bgcolor="#CCCCCC"><font size=-1 FACE="Times New Roman"><a href="$nfile">$nfile</a></font></td>";
					print "<td align=left bgcolor="#CCCCCC"><font size=-1 FACE="Times New Roman" color="#000000">$fsize KB</font></td>";
					print "<td align=left bgcolor="#CCCCCC"><font size=-1 FACE="Times New Roman" color="#000000">$datemod</font></td></tr>\n";
				}				
			}
		}
		print "</table>";
	}	

	else
	{ print "not a valid directory"; }
}
?>

Regards.
Fernado

?>

Posted: Thu Aug 28, 2003 1:17 am
by fernado1283
Hi,
Can anyone please help me, bcauuse I really need to know how to do the drop down list, please. Thanks.

Fernado

Posted: Thu Aug 28, 2003 8:54 am
by JAM
Well, figuring out how to display them in a dropdown, you should be able to manage.

Code: Select all

<select name="filename">
<?php
 for ($i=0;$i=$number_of_files;$i++) {
  echo '<option>'.$filename.'</option>
 }
?>
</select>
<input type="submit">
...or similiar.

The form could be used to send the user to a different page (download.php). In that, you collect the $_POST'ed imagename, start a header()-download.

Read up on header()'s in the manual for examples and tricks.