Jump to content

Recommended Posts

Posted

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

Posted

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);
}
?>

 

Posted
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 :)

Posted

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();
?>

 

Posted

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();
?>

Posted
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);
}
?>

 

 

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...