Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Finding file size (http://www.programmingforums.org/showthread.php?t=12442)

thechristelegacy Jan 25th, 2007 6:48 PM

Finding file size
 
I'm altering a script I found that itterates through a directory and list the files and the file sizes. I changed the script to seach a seperate directory "data". The directory is stored in my currect directory.

The script works fine until I have it search the directory data, it returns the file names, but it doesnt return the filesize. Any hints?

Here is what I have already.

[php]
$theDirectory = "data";
$listDirectories = false;

if(is_dir($theDirectory))
{
echo "<table><tr><td>Name</td><td>Type</td><td>Size</td></tr>";
$dir = opendir($theDirectory);
while(false !== ($file = readdir($dir)))
{
$type = filetype($theDirectory ."/". $file);
if($listDirectories || $type != "dir")
{
echo "<tr><td>" . $file . "</td>";
echo "<td>" . $type . "</td>";
echo "<td>";
if($type == "file")
echo filesize($file);
echo "</td></tr>";
}
}
closedir($dir);
echo "</table>";
}
else
{
echo $theDirectory . " is not a directory";
}
[/php]

When I have the directory set to "." everything works fine, but it's only when I set it to data that I get the trouble.

Thanks :)

DaWei Jan 25th, 2007 8:33 PM

Why don't you put in some debug statements, like this:
[php]
$theDirectory = "cgi-bin";
$listDirectories = false;

if(is_dir($theDirectory))
{
echo '$theDirectory='.$theDirectory."<br/>";
//echo "<table><tr><td>Name</td><td>Type</td><td>Size</td></tr>";
$dir = opendir($theDirectory);
echo '$dir='.$dir."<br/>";
while(false !== ($file = readdir($dir)))
{
echo '<br/>Loop:<br/>';
echo '$file='.$file."<br/>";
echo 'the file name='.$theDirectory.'/'.$file."<br/>";
$type = filetype($theDirectory .'/'. $file);
echo '$type='.$type."<br/>";
if($listDirectories || $type != "dir")
{
//echo "<tr><td>" . $file . "</td>";
//echo "<td>" . $type . "</td>";
//echo "<td>";
if($type == "file")
echo "filesize=".filesize($file)."<br/>";
//echo "</td></tr>";
}
}
closedir($dir);
echo "</table>";
}
else
{
echo $theDirectory . " is not a directory";
}
[/php]
It works for me, mostly, with an occasional failure on specific files. Do you have warnings turned on?
Quote:

$theDirectory=cgi-bin
$dir=Resource id #1

Loop:
$file=.
the file name=cgi-bin/.
$type=dir

Loop:
$file=..
the file name=cgi-bin/..
$type=dir

Loop:
$file=GoogleMap.php
the file name=cgi-bin/GoogleMap.php
$type=file
filesize=7236

Loop:
$file=workMap.js
the file name=cgi-bin/workMap.js
$type=file
filesize=15914

Loop:
$file=PFOMembers.php
the file name=cgi-bin/PFOMembers.php
$type=file
filesize=602

Loop:
$file=test.js
the file name=cgi-bin/test.js
$type=file

Warning: filesize() [function.filesize]: stat failed for test.js in [my server root edited out]/www/filetest.php on line 32
filesize=

grimpirate Jan 26th, 2007 3:09 PM

This isn't the mod of your code but it is similar:
:

function getContainers(){
        $far = array();
        if($handle = opendir($this->gbContainer)){
                while(false !== ($file = readdir($handle))){
                        if($file != "." && $file != ".."){
                                array_push($far, $file);
                        }
                }
                closedir($handle);
        }
        return $far;
}

Of particular importance is the statement highlighted in boldface. You should exclude the '.' and '..' from your list of files as you cannot retrieve a filesize from those, and I'm not so sure their type would be returned as directories. Also if I may recommend using is_file() function rather than the filetype function, that may also help.

DaWei Jan 26th, 2007 3:33 PM

The . and .. directories are tagged as directories. See the output in my post.

grimpirate Jan 26th, 2007 3:40 PM

Ahh I see fair enough in that case let me make the following modification then
:

function getContainers($dir){
        if($handle = opendir($dir)){
                while(false !== ($file = readdir($handle))){
                        if(is_file($dir . '/' . $file)){
                                // Your code goes here
                        }
                }
                closedir($handle);
        }
}


thechristelegacy Jan 29th, 2007 7:20 PM

DaWei, after trying your method, the output is as follows.
Quote:

$theDirectory=data
$dir=Resource id #2

Loop:
$file=2.txt
the file name=data/2.txt
$type=file

Warning: filesize() [function.filesize]: stat failed for 2.txt in /home/savetcom/public_html/dev/usrDir/textTable.php on line 24
filesize=

Loop:
$file=1.txt
the file name=data/1.txt
$type=file

Warning: filesize() [function.filesize]: stat failed for 1.txt in /home/savetcom/public_html/dev/usrDir/textTable.php on line 24
filesize=

Loop:
$file=..
the file name=data/..
$type=dir

Loop:
$file=.
the file name=data/.
$type=dir

Loop:
$file=3.txt
the file name=data/3.txt
$type=file

Warning: filesize() [function.filesize]: stat failed for 3.txt in /home/savetcom/public_html/dev/usrDir/textTable.php on line 24
filesize=
I'm still not sure what's going on, I'm going to look at grimpirate's code next, and see if I can make some sense of what's going on.

Maybe someone could suggest a better way of what I'm trying to do.
Each user will have their own directory, and each directory will have a data folder with all of there data. When they go to their user folder, the page will display all the contents of the data folder with file sizes. I'm only allocating 1 megabyte per user, so I would like to display a filesize for each item they have.

Thanks again :)

DaWei Jan 29th, 2007 8:07 PM

As you'll note from my post, a number of files failed in the same way. I did not investigate. I recommend you have a look at the PHP manual for the function.

grimpirate Feb 2nd, 2007 3:05 AM

I made a small script which will do what you're asking with fewer functions and a simple output.[PHP]<pre>
<?php
$dir = '.';
echo $dir . '/' . chr(13) . chr(10);
$dirContents = scandir($dir);
foreach($dirContents as $dirValue){
$path = $dir . '/' . $dirValue;
if(is_file($path)){
echo ' &laquo; ';
echo str_pad($dirValue, 24, ' ', STR_PAD_RIGHT);
echo str_pad(filesize($path), 24, ' ', STR_PAD_LEFT);
echo ' bytes' . chr(13) . chr(10);
}else{
echo ' &raquo; ';
echo str_pad($dirValue, 24, ' ', STR_PAD_RIGHT);
echo chr(13) . chr(10);
}
}
?>
</pre>[/PHP]In the directory that I tested it in it produced the following:
:

./
 » .                     
 » ..                     
 « Thumbs.db                                  12288 bytes
 « apache_pb.gif                              2326 bytes
 « apache_pb.png                              1385 bytes
 « apache_pb22.gif                            2410 bytes
 « apache_pb22.png                            1502 bytes
 « apache_pb22_ani.gif                        2205 bytes
 « diagonal.php                                543 bytes
 « filesizetest.php                            485 bytes
 « form.php                                    447 bytes
 » forum                 
 « graphics.zip                              58196 bytes
 « grimBase.php                                7374 bytes
 « hash.php                                  10008 bytes
 « hashmat.php                                2073 bytes
 « index.html                                    44 bytes
 « parse.php                                  12299 bytes
 « portal.php                                  725 bytes
 « sort.php                                    2065 bytes
 « sort2.php                                  4404 bytes
 « spiral.php                                  866 bytes
 « test.php                                    165 bytes
 « testerr.php                                10194 bytes


thechristelegacy Feb 4th, 2007 1:35 AM

Hey thanks grimppirate! I was able to fix my orrginal script though.

[php]
// This is the directory to list files for.
$theDirectory = $session->username;
// Do you want to show directories? change to false to hide directories.
$listDirectories = false;
echo "</ br></ br>";
if(is_dir($theDirectory))
{
// Title for top of columns
echo "<table><tr>
<td><u>Name</u></td>
<td><u>Size</u></td>
<td><u>Action</u></td>
<td><u>Action</u></td>
<td><u>Action</u></td>
<td><u>Action</u></td>
</tr>";
$dir = opendir($theDirectory);
while(false !== ($file = readdir($dir)))
{
$type = filetype($theDirectory ."/". $file);
if($listDirectories || $type != "dir")
{
echo "<tr><td>" . $file . "</td>";
// echo "<td>" . $type . "</td>";

echo "<td>";
if($type == "file")
$fileSize = filesize($theDirectory."/".$file)/1024; // Divide 1024 to get into kilobytes
echo number_format($fileSize, 2); // Limit the decimals to two places
echo " Kilobytes.";
echo "</td>";

...
[/php]

The thing that made it work was the
Quote:

$fileSize = filesize($theDirectory."/".$file)
adding the directory to it.

Thanks for all the help guys :) I'm working on a site that will be revieled by a partner and I on March 1st, and I'm not the greatest on php, so I'll be back with more questions.


All times are GMT -5. The time now is 1:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC