Problem with while ($ourItem = readdir($ourDirList))

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

Americanhorizon
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Problem with while ($ourItem = readdir($ourDirList))

Postby Americanhorizon » Sun Dec 18, 2011 1:23 pm

Hi, i'm new in forum e in PHP coding. I've a problem with this simple operation that i've created to found any folder and file into a specific directory

Code: Select all

?php



// declare the folder

$ourDir = "/miaCartella";



// prepare to read directory contents

$ourDirList = @opendir($ourDir);



// loop through the items

while ($ourItem = readdir($ourDirList))

{

   // check if it is a directory

   if (is_dir($ourItem))

   {

      echo "directory: $ourItem <br />";

   }

   // check to see if it is a file

   if (is_file($ourItem))

   {

      echo "file: $ourItem <br />";

   }

}



closedir($ourDirList);



?>


I'm trying this code with easyPHP, but when i launch it, it give me this two errors
Warning: readdir() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP\www\recuperarelistafolderefile.php on line 19

Warning: closedir() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP\www\recuperarelistafolderefile.php on line 47

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Problem with while ($ourItem = readdir($ourDirList))

Postby egami » Mon Dec 19, 2011 6:46 am

I believe your path is wrong. You're using an absolute path, and I believe you're looking for a relative path.

If this directory resides in the current directory of your script, add a period before the forward slash.

Americanhorizon
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Re: Problem with while ($ourItem = readdir($ourDirList))

Postby Americanhorizon » Mon Dec 19, 2011 10:01 am

egami wrote:I believe your path is wrong. You're using an absolute path, and I believe you're looking for a relative path.

If this directory resides in the current directory of your script, add a period before the forward slash.

works, but with some problem. into miaCartella i've placed some jpg. With this code i would want to retrieve the name of those files, instead when i execute it, this is the result:
directory: .
directory: ..

why?

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Problem with while ($ourItem = readdir($ourDirList))

Postby egami » Mon Dec 19, 2011 10:19 am

/miaCartella

This means its at the root of the operating system.
Is this what you want? is this where your files are?

Or are they embedded elsewhere?

Americanhorizon
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Re: Problem with while ($ourItem = readdir($ourDirList))

Postby Americanhorizon » Mon Dec 19, 2011 10:45 am

egami wrote:/miaCartella

This means its at the root of the operating system.
Is this what you want? is this where your files are?

Or are they embedded elsewhere?

no
miaCartella is a folder contained into
C:\Program Files (x86)\EasyPHP\www

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Problem with while ($ourItem = readdir($ourDirList))

Postby egami » Mon Dec 19, 2011 1:47 pm

Then remove the forward-slash at the beginning of the thing, so it looks like..


$ourDir = 'miaCartella/';
Or..

$ourDir = 'C:\Program Files (x86)\EasyPHP\www\miaCartella';
// or however you would write that for Windows....


Return to “Your Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest