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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory listing question

Status
Not open for further replies.

lowtide

Programmer
May 17, 1999
24
US
I am using the following code to extract the images from a directory and place them in a table. It works fine except that the "." and ".." lines from the directory are also read as seperate files. I only want it to show .gif and .jpgs.: <br>
$ptr=opendir($path.$sport); <br>
while ($file = readdir($ptr)){ <br>
print "&lt;TR align=\"left\" valign=\"top\"&gt;&lt;TD&gt;&lt;img src=\" width=\"75\" height=\"100\" border=\"0\"&gt;&lt;/a&gt;&lt;input type=\"radio\" name=\"imgpick\" value=\" <br>
} <br>
closedir($ptr);
 
Have you tried doing an IF inside the while loop which checks if the $file is not "." and ".."? I don't think you can restrict the listing in the opendir command.<br>
<br>
try something like:<br>
<br>
if (strcmp(".", $file) AND strcmp("..",$file))<br>
{<br>
print ...<br>
}
 
This is the way I've done it in Perl. Perhaps there is an equivalent method in PHP. Just a PHP newbie right now so I can't say definitively.<br>
<br>
sub Main<br>
<br>
{<br>
local ($srchdir) = $_[0];<br>
local (*files);<br>
opendir(SRCHDIR, "$srchdir") ¦¦ die "Can't open $srchdir";<br>
@files = grep(!/^\.\.?$/,readdir(SRCHDIR));<br>
while ($file_name = pop(@files))<br>
{<br>
($name,$ext) = split(/\./,$file_name);<br>
if ((-e "$srchdir/${file_name}") && (($ext eq "html") ¦¦ ($ext eq "HTML") ¦¦ ($ext eq "htm") ¦¦ ($ext eq "HTM")))<br>
{<br>
&CopyFile("$srchdir","${file_name}");<br>
}<br>
elsif (-d "${srchdir}/${file_name}")<br>
{<br>
&Main("$srchdir/${file_name}");<br>
}<br>
}<br>
close (SRCHDIR);<br>
}<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top