View Full Version : Dealing with text files in PHP
Mark Hewitt
Fri 7th Jul '00, 7:45am
okay what I need to do is open a text file (not database!) and either store it into an array element per line or go through the file one line at a time so its like
open filename
for every line in file
echo "Stuff";
echo substring of the line;
echo "more stuff";
end for
close filename
Can anyone tell me the php syntax to do that?
Cheers
Herb
Fri 7th Jul '00, 9:35am
heya mark,
if you just want to read and display the file try this in a test php file..
readfile("path/to/file/filename.ext");
This will just read and display it..
Mark Hewitt
Fri 7th Jul '00, 9:42am
Thanks but what I need to do is a bit more complicated than that.
What I have is a text file with a list of stuff - links as it happens.
I need to go through it line by line and for each line add something to the start and something to the end, and actually only use part of the line (substr would work I guess).
So I need access to the lines on an individual basis rather than just printing out the entire file.
Herb
Fri 7th Jul '00, 6:35pm
Ok.. I would need a bit more info as to the structure of the lines.. Maybe post 3 or 4 lines of this file, what you wanted added at begining and end and what you need from each line.. I should be able to give a better example then.. hehe I think..
;)
Mark Hewitt
Sat 8th Jul '00, 4:23am
ok it's something like
<a href="www.com">Link1</a>
<a href="www2.com">Link2</a>
Actually the line format needs to be irrelevant I need a general purpose routine which will let me read a line at a time, manipulate it in some as yet undefined way then let me print it on the screen, then go around and do the same again for the next one until EOF.
firepages
Sat 8th Jul '00, 5:30am
This is a routine which reads a text file a line at a time - I did it to reply to another forum question (www.devshed.com) - this reads password info and checks user/password pairs
but the basics of the file reading are all there,-
"Hi muppet - you will have to lever this into your code - Here we fetch the text line by line (taken straight from the manual) as it is more efficient, we break the loop when a match is found.
<?php
$fd = fopen("passtest.txt", "r");
while (!feof($fd)) {
$buffer = fgets($fd, 4096);//reading 1 lina at a atime//
$split=explode(",",$buffer);//sticking the line in $buffer//
if($split[0]==$PHP_AUTH_USER && chop($split[1])==$PHP_AUTH_PW){$gotit=1;break;}
}
fclose($fd);
if($gotit==1){echo " username $user and password $pass are valid";}else {echo " username $user and password $pass are not valid";}
?>
I think the problem is solved by the chop() fuction which clears the whitespace from the password - without the chop() - the code was not working all the time - give it a try - I was using a textfile as below.
1,mary
2,john
3,bill
4,arthur
5,elsie
The rest of your code is fine and would Authenticate once only as it is."
Simon Wheeler.
Stallion
Sat 15th Jul '00, 3:14am
The file() function will read a file into an array with each line being a new element. Thats how I made my text-file-based news script, and its working pretty well.
bonzo
Sun 16th Jul '00, 7:11pm
heya mark,
i've actually been doing this quite a lot lately.
assuming that your txt file is full of links:
http://domain1.com/
http://domain2.com/
http://domain3.com/ etc...
you can use the following:
$fpcontents = file('filepath');
while ( list($line_num, $line) = each($fpcontents)) {
echo "<a href='$line'>$line</a><br>\n";
}
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.