Jump to content

Use the function dir or opendir


sir KitKat

Recommended Posts

I want to use the function dir to put a list of files in my database,

the list of files is in my www directory.

 

When i use the dir function, i don't get my files but i get in the root folder of the server

 

<?php
$d = dir('/');
echo "Handle: " . $d->handle . "\n<BR>";
echo "Path: " . $d->path . "\n<BR>";
while (false !== ($entry = $d->read())) {
    echo "$entry \n<BR>";
}
$d->close();
?>

 

result:

/./
/../
/selinux/
/lib/
/.lesshst/
/scripts/
/.viminfo/
/sbin/
/home/
/boot/
/usr/
/httpd.conf/
/media/
/access-logs/
/README.archive/
/opt/
/mnt/
/misc/
/lib64/
/net/
/proc/
/etc/
/.autofsck/
/sys/
/.bash_history/
/lost+found/
/tmp/
/.spamassassin/
/quota.user/
/var/
/dev/
/aquota.user/
/bin/
/.autorelabel/
/srv/
/.rnd/
/root/

 

first of all, how do i get my files?

second, how come i see the server files. Isn't that suppose to be hidden ?

 

regards,

sir KitKat

Link to comment
Share on other sites

Are you wanting to list just the files in your root drectory?

 

This will list the files in the directory it's run in.

 

<?php
# NO DIRS
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && is_file($file)) {
            echo "$file<br>";
        }
    }
    closedir($handle);
}
?>

 

Link to comment
Share on other sites

Are you wanting to list just the files in your root drectory?

 

something like that,

 

I want a list of files in the /www/Files/ directory

 

after the list, I set an form to add files to the directory

 

The php code that handles the uploaded file has to

copy the file in the /www/Files/ directory and check if it has to overwrite or to move

 

regards,

sir KitKat

 

Thx, btw, for the quick response :)

Link to comment
Share on other sites

Check my code above and see if that will work for you.

 

 

Here's anther one that will list the files to any directory you set the path to.

 

EDITED for the is_file function

<?php
// Open directory
$path = "/home/byron/public_html/test";
$dir = dir($path);

// List files in directory
while (($file = $dir->read()) !== false)
{
if ($file != "." && $file != ".." && is_file("$path/$file")) {
echo "$file<br />";
}
}
$dir->close();
?>

 

Link to comment
Share on other sites

I changed some code and it works perfect

 

thx :)

 

<?php
//Open directory
$www = "/home/skitkat/www/";
$path = "Files/";
$dir = dir($www.$path);

//List files in directory
while (($file = $dir->read()) !== false)
{
if ($file != "." && $file != ".." && is_file($path.$file)) { // the is_file() function needs a path from the place the code is executed (www for me), not a file name
echo "$file<br />";
}
}
$dir->close();
?>

Link to comment
Share on other sites

the is_file() function needs a path from the place the code is executed (www for me), not a file name

 

Your right, that code didn't have the is_file function in it originally and I just added it without checking from my previous code.

 

This code doesn't need the path added.

<?php
# NO DIR
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && is_file($file)) {
            echo "$file<br>";
        }
    }
    closedir($handle);
}
?>

 

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...