Lowyat.NET Forums

Welcome Guest ( Log In | Register )

LYN wins Intel-Lenovo-Tangs Blogathon challenge. Thank you everybody!
 
RSS feedBump TopicReply to this topicStart new topicStart Poll

Outline · [ Standard ] · Linear+

> reading TXT files in a folder, PHP

Reeson
post Jun 18 2006, 06:39 PM
Show posts by this member only |This post's rating (0+, 0-) | Post #1


Casual
***

Group: Junior Member
Posts: 408
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Sep 2004






is there a function in PHP where it reads all texts (*.txt) files contain in a folder and print them as an output or maybe assign to a variable first before print it..
and if theres a lot of txt file in the folder, what determines which will be read first? is it the file name? unsure.gif
thanks!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
wKkaY
post Jun 18 2006, 06:42 PM
Show posts by this member only |This post's rating (0+, 0-) | Post #2


Smile, what's the use of crying?
Group Icon

Group: Forum Admin
Posts: 4,549
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Jan 2003
From: Malaysia




QUOTE(Reeson @ Jun 18 2006, 08:39 PM)
1. is there a function in PHP where it reads all texts (*.txt) files contain in a folder and print them as an output or maybe assign to a variable first before print it..

2. and if theres a lot of txt file in the folder, what determines which will be read first? is it the file name?  unsure.gif
thanks!
*

1. The primitive function would be readdir().

2. It depends on the order that the filesystem returns them.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
yjwong
post Jun 18 2006, 09:21 PM
Show posts by this member only |This post's rating (0+, 0-) | Post #3


Getting Started
**

Group: Junior Member
Posts: 272
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Jan 2006
From: /usr/bin/php






Just do a directory check using readdir(). Then, using some functions like file() or file_get_contents(), you could get the file contents. Here's an example:

CODE
<?php
// The directory you want to scan the text file for
$dir = "/var/www/";

// Sets the directory pointer
$dp = opendir($dir);

// Read each file into an array
while($files = readdir($dp)){
if(is_file($dir.$files)){
 if(strstr($files, ".txt")){
  $filelist[] = $files;
 }
}
}

foreach($filelist as $file){
$filecontents = file_get_contents($dir.$file);
echo "<pre>";
echo $filecontents;
echo "</pre>";
}
?>
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Bump TopicReply to this topicTopic OptionsStart new topic
 



----debug section please ignore----
Lo-Fi Version Time is now: 23rd November 2009 - 02:19 PM
All Rights Reserved 2003-2009 Vijandren Ramadass (~living on a prayer~)