![]() |
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 :) |
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:
|
This isn't the mod of your code but it is similar:
:
function getContainers(){ |
The . and .. directories are tagged as directories. See the output in my post.
|
Ahh I see fair enough in that case let me make the following modification then
:
function getContainers($dir){ |
DaWei, after trying your method, the output is as follows.
Quote:
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 :) |
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.
|
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 ' « '; 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 ' » '; 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: :
./ |
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:
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