Current location: Hot Scripts Forums » Programming Languages » PHP » List files in dir


List files in dir

Reply
  #1 (permalink)  
Old 04-25-07, 11:22 AM
witakr's Avatar
witakr witakr is offline
Newbie Coder
 
Join Date: Apr 2007
Location: San Angelo, TX
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
List files in dir

How do i obtain and store a list of the txt files stored in a certain directory?

I need to be able to get the name of the files so i can open the file and echo the contents for each txt file


thanks in advance for any advice

j
Reply With Quote
  #2 (permalink)  
Old 04-25-07, 11:36 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:


$files 
glob('path/to/files/*.txt');

foreach (
$files AS $file)
{
    
// Do whatever with $file

Reply With Quote
  #3 (permalink)  
Old 04-25-07, 11:41 AM
witakr's Avatar
witakr witakr is offline
Newbie Coder
 
Join Date: Apr 2007
Location: San Angelo, TX
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Is the asterisk a wildcard? Or do i need to specify a file name? I ned to get all files and store all files and echo the contents of every file
Reply With Quote
  #4 (permalink)  
Old 04-25-07, 11:59 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
It's a wildcard. It will grab all files in the given directory with a .txt extension.

And in the loop you can do:
PHP Code:


echo file_get_contents($file); 
This would echo the content of each .txt file.
Reply With Quote
  #5 (permalink)  
Old 04-25-07, 12:10 PM
witakr's Avatar
witakr witakr is offline
Newbie Coder
 
Join Date: Apr 2007
Location: San Angelo, TX
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
this is my variation and its not echoing anything

PHP Code:

<?php

$files 
glob('$filesdir*.txt');

foreach (
$files AS $file)
{
   echo 
$file;
   
$filecon file_get_contents($file);
   echo 
$filecon;
}

?>
I tried
PHP Code:

echo file_get_contents($file); 

then I changed it to
PHP Code:

   $filecon file_get_contents($file);
   echo 
$filecon
Because it didnt work the 1st time so i tried putting it in a var but i figure thats not the problem...lol

Any idea why it wont work?.. the files are listed in ../scorekeeper/playerfiles
__________________
<?php $status = "confused";
$ptcom = "programingtalk.com/index.php";
if ( !$witakr or $witakr >= $status ) {
$help = fopen("$ptcom", "w+");
echo $help; } ?>
Reply With Quote
  #6 (permalink)  
Old 04-25-07, 12:18 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

$files glob('$filesdir*.txt'); 

Use double quotes instead of single quotes, because variables won't be parsed between single quotes. Besides that, I don't see where $filesdir is set in your code.
Reply With Quote
  #7 (permalink)  
Old 04-25-07, 01:26 PM
witakr's Avatar
witakr witakr is offline
Newbie Coder
 
Join Date: Apr 2007
Location: San Angelo, TX
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
sorry,.. i left that off.. it actually set a little higher in the page in a different php set

Thanks for your help, im going to try this now

EDIT: Alrighty, tested and it works now, thanks for your help. I will sure be back if i get confused again(see sig...lol)
__________________
<?php $status = "confused";
$ptcom = "programingtalk.com/index.php";
if ( !$witakr or $witakr >= $status ) {
$help = fopen("$ptcom", "w+");
echo $help; } ?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dir list filtering extensions id10tn00b PHP 4 08-06-08 12:01 AM
List files from another server kailexx PHP 1 08-27-05 01:37 PM
list all files within a folder+subfolder Caterham Script Requests 0 12-14-04 08:47 AM


All times are GMT -5. The time now is 06:01 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.