Page 1 of 1
ERROR!! FUNCTIONS... GET FILESIZE
Posted: Tue Jul 29, 2003 9:39 pm
by parkin
Hi.. I don't know what I'm doing wrong.. I'm trying to get the filesize and date of a file.. but an error appears... 
The function to get the name of the file
Code: Select all
їphp]<?php
// Open a known directory, and proceed to read its contents
function getFile($dir)
{
//array for the results
$dir_array=Array();
// check if the filename is a directory
if (is_dir($dir))
{
// open the directory
if ($handle = opendir($dir))
{
// read the names of the files in the directory
// loop (end when all the files have been read)
while (($file = readdir($handle)) !== false)
{
// get rid of the filenames that will not need to be displayed
if(($file!=".") && ($file!="..") && ($file!="Drop") && ($file!=".DS_Store") && ($file!="drop box"))
{
$ext = strrchr($file, "."); // check for file extension
if($ext == FALSE) // If doesn't have extension
{
}
else //If have extension
{
$file .= $ext; //add the extension to filename
}
// put the filenames retrieved in the array
$dir_arrayї]=($file);
}
}
return $dir_array; // return the array
closedir($handle);
}
}
}
?>ї/php]
this is the php which displays:
Code: Select all
їphp]<?php
include("directory_function.php");
$dirName = "/Users/test/Sites/document/agreements";
print "dirName: $dirName";
$results_array = Array();
$results_array = getFile($dirName);
print "<table border="1">";
print "<th>
<tr>
<td><b>file name:</b></td>
<td><b>file size:</b></td>
<td><b>date:</b></td>
</tr>
</th>";
// For names to folders
foreach ($results_array as $filename)
{
clearstatcache ();
$Dirfile = $dirName; // dirName (main)
$Dirfile .= $filename; // filename
$Dirfile .= "/";
//check if dir
$dir_is = is_dir($Dirfile);
// if it IS a dir
if($dir_is == TRUE)
{
print "<tr>";
// print the filename
print "<td>";
print "file: $filename<br>";
print "</td>";
//print the filesize
print "<td>";
$size = filesize($Dirfile);
print "file size: $size<br>";
print "</td>";
//print the date
print "<td>";
$datemod = date("m/d/y", filemtime($Dirfile));
print "date: $datemod";
print "</td>";
print "</tr>";
}
// if it IS NOT a dir
Else
{
$file = $dirName; // dirName (main)
$file .= "/";
$file .= $filename; // filename
//print "$. file: $file";
print "<tr>";
// print the filename
print "<td>";
//$ext = strchr($file,"."); // get extention
print "file: $filename<br>";
//print "ext: $ext";
print "</td>";
// print the filesize
print "<td>";
$size = filesize($file);
print "file:$file<br>";
print "file size: $size<br>";
print "</td>";
// print the date
print "<td>";
$datemod = date("m/d/y", filemtime($file));
print "date: $datemod";
print "</td>";
print "</tr>";
}
}
print "</table>";
?>ї/php]
The errors i get for filesize:
(The first file works fine.. The filesize is displayed.. But after that.. An Error occurs)
Warning: stat failed for /Users/test/Sites/document/agreements/G4 Nomenclature (20 Apr.sit.sit (errno=2 - No such file or directory) in /Users/test/Sites/engg/php_test/directory.php on line 93
The errors i get for date:
(The first file works fine.. The filesize is displayed.. But after that.. An Error occurs)
Warning: stat failed for /Users/test/Sites/document/agreements/G4 Nomenclature (20 Apr.sit.sit (errno=2 - No such file or directory) in /Users/test/Sites/engg/php_test/directory.php on line 100

Hope someone can help me out..

Posted: Tue Jul 29, 2003 11:23 pm
by m3rajk
it's trying to read a file it doesn't have permission to read. that's my guess. that or there's a remnant left from something (ie symbolic link and the file it points to is gone) that's causing it to have an issue. check for those and let us know.
...
Posted: Wed Jul 30, 2003 12:53 am
by kettle_drum
Make sure the file path is correct and that the file does exist at that location, just echo the file path to the page so you can check. Then also make sure that the file path is starting from the / directory and not your documentbase.
Posted: Wed Jul 30, 2003 1:48 am
by parkin
m3rajk.. sorry.. I dun really understand what you're trying to say...
kettle drum.. i've checked the directory.. It's works ok.
The error only displays when it goes into the else statement (this is to print for files)
there is nothing wrong when it goes into the if statement (this is to print for the folders)
regards...
Posted: Wed Jul 30, 2003 10:21 am
by m3rajk
is this a posix (unix/linux/macosx+/bsd/etc i get the feeling it is one) or windows environment that you're running on?
also, i think you may have written a function that already exists. remember, there's more than file and directory. you also have links, and in /dev/ there's a other things as well that often appear.
these three links should help you
http://us4.php.net/manual/en/function.is-file.php
http://us4.php.net/manual/en/function.is-dir.php
http://us4.php.net/manual/en/function.is-link.php
don't blow them off. read them, then re-read what i wrote, because that should be clarified by those three links, if not, and you are on a posix system, then ask your admin if you can have 30 minutes to play question and answer with him. then when you have it, sit down, type "ls -la" and hit return. point to the line that's something like
-rwxw-xr-x
and ask him what the first dash of that area signifies and what the symbol can mean about the tpe of think in the directory.
this is the best way i can think of fo ryou to learn what i mean by link and a remnant that's left over.
(note, the first is - for files, l for links, d for directories and there's a bunch of other things for special cases that's not something to get into right now)
ERROR!! FUNCTIONS... GET FILESIZE
Posted: Thu Jul 31, 2003 7:53 pm
by parkin
Hey.. Thanks..

The function works.. I think the problem is because I didn't put the .php in the directory I wanna use.. I've changed the $dir to $dir="./" (current directory) and so far.. It works fine..
Another question to ask though.. I would like to go through the directory.. Display the filenames in it.. And if the files are sub-directories.. I would like to turn the names into a link (that I've done) and everytime the user clicks on a sub-directory link.. The files of the sub-directories will display..
The whole idea is to make this "automatic" so next time.. when someone adds a new file to the folder.. It will be displayed.. No editing of .php or html codes would be needed to place the link on the web..
Would I need the function to be recursive?
Correct me if I'm wrong.. I'm thinking yes.. I'll need a recursive function.. I'll need to place it in the IF statement which checks if the file is a directory.. But I don't know what would be the exit statement...
here is the function:
Code: Select all
їphp]<?php
function getFile($dir)
{
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") //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=filesize($nfile)/1000; //get filesize
$datemod = date("m/d/y", filemtime($nfile));
if (is_dir($nfile)) // the file is a directory
{
print "<tr><td><a href="$nfile">$nfile</a></td>";
print "<td>Directory</td>";
print "<td> </td></tr>\n";
}
else // the file is not a directory
{
print "<tr><td><a href="$nfile">$nfile</a></td>";
print "<td>$fsize KB</td>";
print "<td>$datemod</td></tr>\n";
}
}
}
}
else
{ print "not a valid directory"; }
}
?>ї/php]
here is the .php page:
Code: Select all
їphp]<?php
include("doc_function.php");
print "<html>
<body>";
print "<table align=center bgcolor=#ffffff cellpadding=4 cellspacing=0 border=2>";
print "<tr>
<th><b>File</b></th>
<th><b>Size</b></th>
<th><b>Date</b></th>
</tr>";
$dir="./"; // current directory
print "dir: $dir";
getFile($dir);
print "<body>
</html>";
?>ї/php]
Thanks a WHOLE LOT

Posted: Thu Jul 31, 2003 9:02 pm
by parkin
Ooohh.. Okay..

I've ended up doing something different..
I thought.. since the .php and function needs to be in the directory to display the files of that directory.. What if I copied those two files and placed in all the directories that I want to display.. And edited the code for the links only?
like in the function:
Code: Select all
їphp]<?php
...
print "<tr><td><a href="./$nfile/documentation.php">$nfile</a></td>";
...
?>ї/php]
Well.. It does work.. But is there a better way of doing it?

Thanks for any help or comments..

Posted: Thu Jul 31, 2003 10:29 pm
by parkin
i have another problem.. If I want to display on the page.. A title which is the name of the directory that I'm displaying the contents.. How do I display it? If I'm using $dir="./" (currenty directory)
i don't want to change it to this way "/name/name/name/" because I want to have all the folders to use the same .php file.. As in no codes needs to be changed when I add in a new directory and copy the .php and functions into there..
Oh well.. *sigh*

Posted: Thu Jul 31, 2003 11:51 pm
by DuFF
Maybe
getcwd() will help? (Get Current Working Directory).
Posted: Fri Aug 01, 2003 12:06 am
by parkin
Yup..

It helps ALOT!
thanks duFF!
