Changing directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
G_Man
Forum Newbie
Posts: 3
Joined: Tue Oct 29, 2002 12:49 am

Changing directories

Post by G_Man »

Im kinda new to PHP and am trying to make a file manager and im stuck on how to change the directory i am in this is what i have now (keep in mind I new and it works sugjestions welcome :D )

Code: Select all

<?
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
$path = "/home";
$path_name = "webmaster";
$path_full = "$path/$path_name";

chdir ( $path_full );
$cur_dir = opendir( $path_full ) or die ("unable to open Directory $path_full");
while (false !== ($file = readdir($cur_dir)))
&#123;
    if(is_dir ($file)) &#123;
        $dirs&#1111;] = $file;
    &#125;
    else &#123;
        $files&#1111;] = $file;
    &#125;
&#125;
closedir($cur_dir);

$filemodtime = date("F j Y h:i:s A", filemtime( $file ));

function my_filesize($file) &#123;

$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte

$size = filesize($file);

if($size < $kb) 
	&#123;
	return $size." B";
	&#125;
else if($size < $mb)
	&#123;
	return number_format($size/$kb,2)." KB";
	&#125;
else if($size < $gb)
	&#123;
	return number_format($size/$mb,2)." MB";
	&#125;
else if($size < $tb)	
	&#123;
	return number_format($size/$gb,2)." GB";
	&#125;
else	
	&#123;
	return number_format($size/$tb,2)." TB";
	&#125;
&#125;

?>
and do the list with

Code: Select all

<?
      foreach ($dirs as $file) &#123;
          echo "<tr><td><img src="imgs/directory.gif"></td><td><a href="test.php?path_name=$path_name/$file">$file</a></td><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
      &#125;
      foreach ($files as $file) &#123;
		  if(ereg(".exe", $file))
			  &#123;
			  $my_size = my_filesize($file);
			  echo "<tr><td><img src="imgs/executable.gif"></td><td><a href="$path_name/$file">$file</a></td><td>$my_size</td><td>$filemodtime</td></tr>\n";
		   	  &#125;
		  else if(ereg(".bmp", $file) || ereg(".jpg", $file) || ereg(".jpeg", $file) || ereg(".gif", $file))
			  &#123;
			  $my_size = my_filesize($file);
			  echo "<tr><td><img src="imgs/image.gif"></td><td><a href="$path_name/$file">$file</a></td><td>$my_size</td><td>$filemodtime</td></tr>\n";
			  &#125;
		  else if(ereg(".html", $file) || ereg(".htm", $file) || ereg(".php", $file) || ereg(".txt", $file) || ereg(".css", $file))
			  &#123;
			  $my_size = my_filesize($file);
			  echo "<tr><td><img src="imgs/text.gif"></td><td><a href="$path_name/$file">$file</a></td><td>$my_size</td><td>$filemodtime</td></tr>\n";
			  &#125;
		  else if(ereg(".mp3", $file) || ereg(".wav", $file) || ereg(".midi", $file) || ereg(".wma", $file) || ereg(".aif", $file))
			  &#123;
			  $my_size = my_filesize($file);
			  echo "<tr><td><img src="imgs/audio.gif"></td><td><a href="$path_name/$file">$file</a></td><td>$my_size</td><td>$filemodtime</td></tr>\n";
			  &#125;
		  else
			  &#123;
			  $my_size = my_filesize($file);
			  echo "<tr><td><img src="imgs/unknown.gif"></td><td><a href="$path_name/$file">$file</a></td><td>$my_size</td><td>$filemodtime</td></tr>\n";
			  &#125;
      &#125;
	  ?>
now i need a way to have it so when you click a directory it will change to the new directory and the way i have it now (should) keep working no matter how far down the tree you go

Code: Select all

<a href="$path_name/$file">$file</a>
thank you in advance

see the page @ http://24.116.67.44/test.php

and no im not stupid that is just the apache install folder not my real apache folder :P
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Use the directory functions:-

Code: Select all

chdir - change directory
chroot - change the root directory
dir - directory class
closedir - close directory handle
getcwd - gets the current working directory
opendir - open directory handle
readdir - read entry from directory handle
rewinddir - rewind directory handle
Check them out on the manual.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

since there is already this code in
chdir ( $path_full );
$cur_dir = opendir( $path_full ) or die ("unable to open Directory $path_full");
you probably want to know how to make use of the user's selection....
my tip is: Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2 and urlencode()
G_Man
Forum Newbie
Posts: 3
Joined: Tue Oct 29, 2002 12:49 am

Post by G_Man »

I have been looking at there docs for almost 2 days now and they dont seem to have anything about changing the directory and re-listing, but im starting to think theres something wrong with my php install because when i tryed to do a debug on it by puting a line in to show me if the $path_name is changing and going to http://24.116.67.44/test.php?path_name=A_FOLDER_NAME and it would not change. and i saw a thing @ the php.net faq and saw that i should turn on register_globals but i dont remember needing this ti be on back befor I installed this version . any ideas?? :?: :?: :?: :?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$path = "/home";
$path_name = "webmaster";
$path_full = "$path/$path_name";
if the user data was assigned to those variables they're gone by now ;)

try

Code: Select all

echo phpversion();
echo '&lt;pre&gt;';
echo '$_GET : '; print_r($_GET);
echo '$HTTP_GET_VARS : '; print_r($HTTP_GET_VARS);
echo '&lt;/pre&gt;';
phpinfo();
to find out more about your php installation
http://www.php.net/manual/en/reserved.variables.php
G_Man
Forum Newbie
Posts: 3
Joined: Tue Oct 29, 2002 12:49 am

Post by G_Man »

I shoud take this time bucause im not sure what u mean by "users" to tell you im using WinXP PRO with apache 2.0.39 and php 4.2.2
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

with user's selection I meant the data that is sent by clicking one of the links

plz follow this link: Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2
Post Reply