Jump to content


Accessing a text file

  • Please log in to reply
3 replies to this topic

#1 pmorrison

  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 March 2006 - 07:01 PM


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.



$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
} }

#2 craygo

  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 06 March 2006 - 07:23 PM

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


#3 pmorrison

  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 March 2006 - 07:34 PM

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.


#4 pmorrison

  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 07 March 2006 - 08:53 PM

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?


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users