Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 25th, 2007, 5:48 PM   #1
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 707
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
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
thechristelegacy is offline   Reply With Quote
Old Jan 25th, 2007, 7:33 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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=
__________________
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
DaWei is offline   Reply With Quote
Old Jan 26th, 2007, 2:09 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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.
__________________
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
grimpirate is offline   Reply With Quote
Old Jan 26th, 2007, 2:33 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jan 26th, 2007, 2:40 PM   #5
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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
grimpirate is offline   Reply With Quote
Old Jan 29th, 2007, 6:20 PM   #6
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 707
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
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
thechristelegacy is offline   Reply With Quote
Old Jan 29th, 2007, 7:07 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 2nd, 2007, 2:05 AM   #8
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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
__________________
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
grimpirate is offline   Reply With Quote
Old Feb 4th, 2007, 12:35 AM   #9
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 707
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
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.

Last edited by thechristelegacy; Feb 4th, 2007 at 12:36 AM. Reason: typo
thechristelegacy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:51 AM.

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