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();
?>

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.

<?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();
?>

 

Thank you :D

 

{resolved}

Archived

This topic is now archived and is closed to further replies.

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