Forum Moderators: coopster

Message Too Old, No Replies

PHP File-system functions: realpath()

         

anshul

6:01 am on May 11, 2005 (gmt 0)

10+ Year Member



realpath() not gives real path of directory.
e.g., realpath('phpmyadmin'); should be c:\Inetpub\wwwroot\phpmyadmin I got it c:\Inetpub\wwwroot\test\phpmyadmin because my script is in test directory! How can I get correct path?
I'm using Apache2 on Windows 2003.

Stormfx

6:38 am on May 11, 2005 (gmt 0)

10+ Year Member



Are you using per-site configuration with a virtual directory?

anshul

7:10 am on May 11, 2005 (gmt 0)

10+ Year Member



I intend to write directory browsing facility using PHP. Some code I've written:
$handle=opendir('/');

echo "<strong>Directories:</strong><br>";
while($file=readdir($handle)) {
if(is_dir($file))
echo "<a href='file_browser.inc.php?dir=$file'>".$file."</a><br>";
}

rewinddir($handle);
echo "<br>";

echo "<strong>Files:</strong><br>";
while($file=readdir($handle)) {
if(is_file($file))
echo $file."<br>";
}

closedir($handle);
?>


I'm struggling with this for many hours, I need yours' help, very much.

Stormfx

4:14 pm on May 11, 2005 (gmt 0)

10+ Year Member



Change:

while($file=readdir($handle)) {

To:

while(false!== ($file = readdir($handle))) {

in both places that it appears.

"Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named "0")."

As mentioned here:
[php.net...]

anshul

6:05 pm on May 11, 2005 (gmt 0)

10+ Year Member



As I intend to write Web directory script, I need pass a $_REQUEST[] to opendir() so that when a directory is clicked, its files and directories are listed. What's the querystring.

Stormfx

10:54 pm on May 11, 2005 (gmt 0)

10+ Year Member



Well, you'd do something like:

display.php?dir=dir_name

then grab that data by using $_GET['dir_name']

However, I should warn you that if the application has permissions outside of the web root, it will give them a list of whatever directory they enter.

For example, someone could manually type in:

display.php?dir=path_to_system_files

So you'll have to find some way of validating it. For example, you could check the name, make sure it has no slashes, etc. Then, make sure the directory exists in the restricted directory before opening it.

anshul

7:48 am on May 12, 2005 (gmt 0)

10+ Year Member



Yeah! I got is working; I used getcwd() and chdir() Now I wanna enhance my script i.e., allow create/edit/delete/upload files. And a recycle-bin-type directory. I used fileperms() to get information about file/directory permissions. It is giving 33206 for all files and 16895 for all directories. I expected it to return something like '0777' or '0666' or '777' or '666'. I'm using Windows so I also use base_convert(), still couldn't get good. date ("F d Y H:i:s.", filemtime($filename)); behaved well on Windows. Please reply. Thank you, for taking interest in this thread.

anshul

7:12 am on May 13, 2005 (gmt 0)

10+ Year Member



Please, someone tell how to get information about file/directory permissions. I used fileperms($file), but I get number '33206' for all files and a different same number for all directories. I used this function on Windows/Fedora with same results. I expected something like '0666', '0777', '666', or '777'

coopster

12:10 am on May 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Did you try formatting the output?
echo substr(sprintf('%o', fileperms($file)), -4);