Jump to content

Archived

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

pmorrison

Accessing a text file

Recommended Posts

Hi,

I am trying to access a text file which contains a list of postcodes, each on a separate line. I want to go through this file, checking to see if the postcode entered is valid. I tried using it, but realised that it wasnt searching the file correctly. I have put in the line echo "$numLines"; to see how many lines it says are in the text file, it says there is only 1, but I know there are more. Am I trying to access the file incorrectly? I have put the list.txt file in the same directory as the php file.

Cheers,

Paul

$filename = "/list.txt"; // File which holds all data
$inputString = $postcode;
$arrFp = file( $filename ); // Open the data file as an array
$numLines = count( $arrFp ); // Count the elements in the array

$numWords = count( $arrWords ); // Count the words in the string

for($j=0; $j<$numLines; $j++) {
echo "$numLines";
$outputstring = "not found";
if(strstr($inputString, trim( $arrFp[$j] ))) {
$errorlist = errorlist('Please fill in the "Postcode" field with a valid postcode', $errorlist);
$errorform = 1;
$postcodestyle = ' ; color:red';
$outputstring = "found it";
}
echo $outputString; // Echo the string replacing restricted words
} }

Share this post


Link to post
Share on other sites
no need to put in the slash before the file if it is in the same directory. putting the slash in makes php go back to root.

try that first

Ray

Share this post


Link to post
Share on other sites
Hi Ray,

I tried it without the / but it then doesnt load the page when I click on the submit button, I get a blank page with just the header. However, when I comment out the $arrFp = file( $filename ); line it loads Ok, but obviously doesnt look at the file.

Paul

Share this post


Link to post
Share on other sites
I have been looking into using text files with PHP and I am wondering if I need some kind of delimiter to show PHP that it is a new line. Currently each postcode is on a separate line, but just separated using a usual return, no \n or any other delimiter. Could this be a problem?

Paul

Share this post


Link to post
Share on other sites

×

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.