Php Text File Error

liunx

Guest
Hi,<br /><br />I just starting learning about php reading text files, and I wrote a script, and then tested it on my server and I get this error message:<br /><br />Warning: fopen(notes/data/names.txt) [function.fopen]: failed to create stream: No such file or directory in c:\inetpub\wwwroot\testwritepage.php on line 10<br />Couldn't open the data file. Try again later.<br /><br />Here's my script: <br /><br /><?php<br />$fp = fopen( "notes/data/names.txt", "w" );<br /><br />if(!$fp)<br />{<br /> echo "Couldn't open the data file. Try again later.";<br /> exit;<br />}<br />?> <br /><br /><br />I don't understand why I'm getting errors already for such a simple lines of code that I copied and pasted from a tutorial site. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/unsure.gif" style="vertical-align:middle" emoid=":unsure:" border="0" alt="unsure.gif" /> Any help would be appreciated as always. Thanks. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
While I am also learning,<br />questions..<br />$fp = fopen( "notes/data/names.txt", "w" );<br /><br />are you trying to <b>read</b> the file? "<b>r</b>"<br />and does the folder <b>notes</b> exist below the current folder the test code is in?<br />and then <b>data</b> below that?<br /><br />maybe try using a file in the same folder to start with.<br /><br />But as i say, I am still learning<!--content-->
try changing this:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$fp = fopen( "notes/data/names.txt", "w" );<!--c2--></div><!--ec2--><br /><br />to this:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$fp = fopen( "./notes/data/names.txt", "w" );<!--c2--></div><!--ec2--><!--content-->
Hey! thanks for replying everyone! <br /><br />You're right, I forgot to create the folder. Now that I did, I opened the page again on my web browser and now I get a blank page? <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/huh.gif" style="vertical-align:middle" emoid=":huh:" border="0" alt="huh.gif" /> What's happening now?<!--content-->
Nevermind...I got it to read the text file now after doing some reading and research. But I have one question though... <br /><br />Say that on the text file, I have a list of words on there, so on my web browser, how do I get it to print one word from the text file, then next line, then the next word and so on and so on? cause right now, it's printing all the words on one line. <br /><br />Any help would be appreciated, thanks again everyone! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
I'm going to assume each word you want on a newline is on a newline in the text file... There might be better ways, but what I just do is read the entire file into an array, and then just go through the array and print out each element. Like...<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$file_array = file("/path/to/file.txt");<br />while (list($i, $val) = each($file_array)) {<br />   echo "$val<br>";<br />}<!--c2--></div><!--ec2--><br /><br />Or using your file pointer ($fp) try this.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->while (!feof($fp)) {<br />   $nextline = fgets($fp, 1024);<br />   echo $nextline . "<br>";<br />}<!--c2--></div><!--ec2--><!--content-->
 
Back
Top