![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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 ![]() |
|
|
|
|
|
#2 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#3 |
|
King of Portal
|
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;
}
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The . and .. directories are tagged as directories. See the output in my post.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
King of Portal
|
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);
}
}
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#6 | |
|
Expert Programmer
|
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 ![]() |
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
King of Portal
|
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: ./ » . » .. « 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
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#9 | |
|
Expert Programmer
|
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.Last edited by thechristelegacy; Feb 4th, 2007 at 12:36 AM. Reason: typo |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| converting string to float | beginnerCCC | C | 22 | Oct 2nd, 2006 11:59 PM |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 8:55 AM |
| After execution - Error cannot locate /Skin File? | wchar | Visual Basic | 1 | Mar 5th, 2005 9:04 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |
| Structure char field to a disk file | ehab_aziz2001 | C++ | 0 | Feb 10th, 2005 2:42 PM |