Jump to content

Problem with Listing files and their information in a directory


Recommended Posts

Hello folks,

I am trying to list all the files present in my files directory and provide links so that they can be downloaded. Below is the code which is giving me some problems. Please help me out with this.

 

Problems:

1)filetype(), filemtime(), filesize() are giving warnings like lstat failed for SOME_FILE* in .... stat failed for SOME_FILE* in ....

 

2) When I click on the link of filename, I get a new window which says 404 Not Found Error. "The requested URL /SPR_ISA/Resource was not found on this server."

 

I think I am doing something wrong with path specification.


<TABLE BORDER="5" BORDERCOLOR="green" CELLSPACING="0" CELLPADDING="2">
<TR BGCOLOR="#006600">
	<TD><h6>File Name</h6></FONT></TD>
	<TD><h6>File Size (bytes)</h6></FONT></TD>
	<TD><h6>Date Created</h6></FONT></TD>
	<TD><h6>File Type</h6></FONT></TD>
</TR>

<?php

//Open a directory
$TrackDir = opendir("files/");
     
//Read all files in the directory.
while ($file = readdir($TrackDir)) 
{
if ($file == "." || $file == "..") 
        { } 
else
{
         //Find out the file type from extension.
	$fileType= substr(strstr($TrackDir.$file,"."),1);
	if($fileType==null)
	switch($fileType)
	{
		case "doc":
			$fileType="Word Document";
			break;
		case "txt":
			$fileType="Text File";
			break;
		case "pdf":
			$fileType="Adobe PDF";
			break;
		case "zip":
			$fileType="ZIP File";
			break;
		case "rar":
			$fileType="ZIP File";
			break;
		case "null":
			$fileType="Folder";
			break;
	}

//Provide link to download the file and also some info about the file.
	$path = $TrackDir . $file;
	echo "<tr><td><a href=$path target=_blank>$file</a> </td>";
	echo "<td><font color=blue>	" . filesize($path)."</font></td>";
	echo "<td><font color=blue>"	. date("d M Y", filemtime($path)	.	"</font></td>";
                echo "<td><font color=blue>  ".$fileType."</td></tr><br>";         
}   
} 
closedir($TrackDir); 

?>

 

Please let me know my mistakes...

Thank You.

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.