Web Hosting Talk







View Full Version : PHP fopen


xerocity.com
08-27-2002, 02:56 AM
Hello,

I would like to know if anyone out there can give me an example of code for what I need. I have tried all day today and it still will not work correctly.

What I need is some php code that will open a textfile and scan the entire textfile line by line (it is *not* a delimited file but each record is on its own line) and if it matches a variable then do nothing. But if there are no lines that match, append the variable to the end of the file.

It should be pretty simple but for some reason it isn't working right. I know that I can do this in perl (for example) but it is must be done in php.

It will be placed inside of a while statement which will change the variable and use the function multiple times.

Thank you in advance.

ranchoweb
08-27-2002, 03:25 AM
Hi, you don't need fopen for this. the "file" function will put the whole file into an array.

$stuff = file("/path/to/textfile.txt");
for ($i=0;$i<count($stuff);$i++) {
//this will loop through each line...
//do your comparisons here
}

combs
08-27-2002, 01:33 PM
Right. Put the whole file in array and loop through the array. Set a boolean flag to false initially. If the line matches, make it true in loop. If after the loop , flag is false that means no match were found and hence append the line in the file.

xerocity.com
08-27-2002, 07:04 PM
Originally posted by ranchoweb
Hi, you don't need fopen for this. the "file" function will put the whole file into an array.

$stuff = file("/path/to/textfile.txt");
for ($i=0;$i<count($stuff);$i++) {
//this will loop through each line...
//do your comparisons here
}

This is basically one of the pieces of code that I tried. However, what will put the current line into a variable so I can compare it? I may be incorrect but $i will only hold the line number.

Thank you again.

ranchoweb
08-27-2002, 07:27 PM
Oh sorry, to access the current line, just use $stuff[$i]

Like so:

$stuff = file("/path/to/textfile.txt");
for ($i=0;$i<count($stuff);$i++) {
print $stuff[$i];
}

xerocity.com
08-27-2002, 07:29 PM
Originally posted by xerocity.com


This is basically one of the pieces of code that I tried. However, what will put the current line into a variable so I can compare it? I may be incorrect but $i will only hold the line number.

Thank you again.

Never mind I figured it out.

Thanks.

xerocity.com
08-27-2002, 07:31 PM
ranchoweb,

I guess I figured it out at the same time you posted the answer.

Thanks again,

ranchoweb
08-27-2002, 07:37 PM
:)

apokalyptik
08-27-2002, 08:17 PM
<?php

$datafile='/path/to/file';

/* Read each line that the file contains into an array - $data */
$data=file($datafile);

/* Loop through data */
foreach ( $data as $idx => $val ) {
echo 'Line #';
echo $idx;
echo ' Says: "';
echo $val;
echo '"<br>';
}

/* pull out a specific entry */
echo 'Line #2 Says: "';
// remember that arrays start at element # 0
echo $data[1];
echo '"<br>';

?>


Please note that i wrote the above DIRECTLY into the reply, i did _NOT_ debug this at all... its just an example... I find foreach loops easier to deal with...