Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

readdir() problem

Status
Not open for further replies.

ronnyjljr

IS-IT--Management
Nov 23, 2003
249
US
Hi!

I have a problem with scanning a directory for files.
If there are less than 3 files in the directory, the
script quits executing. Any thoughts?
This is how I am scanning my directory; note that I have a check to exclude filenames less than 3 characters to get rid of the . and .. entries. It echoes <option> brackets because I display the results in a list box.


Code:
[blue]
	 $myDir = opendir('/BlahDir');
	while (false !== ($file = readdir($myDir)))  
	 {
		if (strlen($file)>3) echo "<option value =\"$file\">$file</option>";
	 } [green]//END WHILE[/green]
		closedir($myDir);
[/blue]

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
can you show us the contents of the directory without checking the length?
Also if you have a fiel called xx it will get missed. I would suggest you check for the name being . or .. instead
 
Try this,

if ($dh = opendir($dir)) {
while (($file = readdir($dh)) != false) {
if (
(is_file($dir."/".$file) &&
(strcmp(".", $file)) && (strcmp("..", $file))
)
{
print "file = $dir/$file\n";
}
}
close($dh);
}

This prints all files in a directory, skipping the "." and "..", you can make it recursive by checking is_dir with a else statemnt
 
Ingresman,


The only items that show up in the directory are:

Code:
.
..
blank.doc
code.doc

[green]//Only Word documents are to be stored in this folder, thus why I set the strlent to 3,  because the length is at a minimum 3, or rather 4.[/green]

Rhymejerky,
I will try your method here in a few


cout << "If you don't know where you want to go, we'll make sure you get taken";
 
If you want to check whether you have .doc, you can do somehting like

if (!strcmp(substr($file, -4), ".doc")) {
print "I got .doc file\n";
}
 
Hi guys,

Ok, both methods produce the same result.
Only .doc files will be in this folder because I have control over how they are uploaded and I check for it.
Anyways, any script that iterates through any directroy that has less than 3 entries does not work. And I have also discovered that if there are over 8 entries it also does not work. By not working I mean the script stops executing when it hits the while loop and nothing is produced.

Thanks,
Ron

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
I just ran the same script I posted (with a few syntax fixes) and it displayed fine on my machine. Can you run the following code in the directory that has all the .doc file. Also, make sure your dir path is good.

Code:
<?

$dir = ".";

if ($dh = opendir($dir)) {
  while (($file = readdir($dh)) != false) {
    if (
         (is_file($dir."/".$file)) &&
         (strcmp(".", $file)) && (strcmp("..", $file))
       )
    {
      print "file = $dir/$file\n";
    }
  }
  fclose($dh);
}

?>
 
Interestingly enough it works on my .com.
However the server I am currently working doesn't want to play nice. I am assuming it is a server config so I will email the admin.

Thanks guys,
Ron

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top