Jump to content

List all .txt files in directory


peter_anderson

Recommended Posts

<?php
//Open directory
$dir = dir("my/directory");

//List files in directory
while (($file = $dir->read()) !== false){
//Make sure it's a .txt file
if(strlen($file) < 5 || substr($file, -4) != '.txt')
	continue;

echo "filename: " . $file . "<br />";
}

$dir->close();
?>

Link to comment
Share on other sites

Hello,

 

I am trying to load the name of all .txt files within the directory, excluding one.

 

I cannot think of the best way to approach it (if any)?

 

Can anyone help?

Thanks

 

You most likely use a for loop or a foreach loop depending if you're dealing with an array or not. You might also want to heck out the substr() function, the is_file() function, is_dir() function, scandir() function. That should get you started.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.