Jump to content

If statement to remove file name from displaying in php list


jonw118
 Share

Recommended Posts

I am using the following php script to display all the files that are uploaded in a directory on a page. There are two files in the folder I do not want displayed: .htaccess & index.php.

 

Can someone help me out and provide a little guidance about what I need to do to print all the file names EXCEPT for those two. I imagine some sort of if statement.

 

CODE:

 

Top of page:

<?php
$dirname = ".";
$dir = opendir($dirname);
?>

 

Body:

<?php
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
echo("<a href='$dirname$file'>$file</a> <br />");
}
}
?>

 

Thanks so much!

Link to comment
Share on other sites

if($handle = opendir('.')){
while(false !== ($file = readdir($handle))){
	if($file != '.' && $file != '..' && $file != 'index.php' && $file != '.htaccess'){
		echo '<br />
<a href="'.$file.'">'.$file.'</a>';
	}
}
closedir($handle);
}

Link to comment
Share on other sites

This thread is more than a year old.

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.   Restore formatting

  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.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.