PHP: File handling

S

(Shovel)

Guest
I'm learning... hence no mySQL on this site.
Also, because I don't have enough mySQL databases to give away to the guy I'm making this for.

In principal it's all going fine, but I'm getting an inexplicable error which I can't figure out, and searching google only gives me lots of sites that are suffering from the same error.. ahem.

Here's the entire block of code:
Code:
		 if($submit == "insert") {
		 		/* Assemble File */
				if(is_null($headline) || is_null($body)) { /* Check for null values */
					echo("<h1>Error, you must enter a headline and body text</h1>");
					break;
			  }
				$news = array($date,
					      $username,
				              $headline,
					      htmlspecialchars($body));
				$record = implode("|", $news);
				$nf = file("../data/news");													
		 	  /* Determine insert or update */
				if($update == -1) {
				  /* Insert */
					array_unshift($nf,$record);
					debug("The news file now contains: $nf[0]");
				}
				else {
				  /* Update */
					$nf[$update] = $record;
				}
				/* Write back to file */
				$nf_ptr = fopen("../data/news",'w') || die("Could not open /data/news");
				foreach ($nf as $line) {
								fputs($nf_ptr, "$nf[0]\r\n");			
								debug("Writing $line\n");
				}
				fclose($nf_ptr);
		 }

The problem occures in the "Write back to file" area. $nf, stands for "news file" and is an array of news entries, and $nf_ptr is the associated file pointer for the file on disk.

Oh, and "debug" is a little function that prints to screen, it's separated into a function so that I can disable the output when it goes live.


Anyway, here's what actually happens though:
Code:
Warning: fputs(): supplied argument is not a valid stream resource in d:\bmpw\documents\webdev\local_http\control\gc.php on line 106
Writing 1055255181|/|Test News 1q|Hello!
Warning: fclose(): supplied argument is not a valid stream resource in d:\bmpw\documents\webdev\local_http\control\gc.php on line 109

The opens fine, and it gets created in the right directory, but it doesn't get written to. have also tried doing the file in the same directory as the PHP file and had the same lack of luck.

Earlier in the script, I wrote another bit of code, using a different file (in the same directory as the file) and that wrote fine. Therefore, I'm confused :(

Meep?
 
S

(Shovel)

Guest
*bump*

I don't suppose anyone does know the answer to this? It's been a week and I've gone back to it and it's doing my head in :(
 
J

Jonty

Guest
Hi Ben

I'm honestly not sure what's wrong, since I rarely use the write to file methods in PHP. Have you checked the PHP manual? There's a sample script in the fwrite() entry which seems to include some nice error checking, which may help to narrow down exactly where the problem lies . . .

Code:
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         print "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (!fwrite($handle, $somecontent)) {
        print "Cannot write to file ($filename)";
        exit;
    }
    
    print "Success, wrote ($somecontent) to file ($filename)";
    
    fclose($handle);
					
} else {
    print "The file $filename is not writable";
}
?>
Other than that, have you tried enabling error reporting on your local PHP setup? I know most PHP errors can be a little obtuse, but it may help to highlight exactly what's wrong.

Sorry I can't be of more help.

Jonty

<plug> P.S. If you do want to switch to a database driven solution, <noname> sql could provide you with the necessary MySQL database :D</plug>
 
S

(Shovel)

Guest
I do already have mySQL on my webserver I'm afriad Jonty, though they only offer 2. I might investigate whether one of them is in use or not (I share it with friends) and open that up as a misc database for random shite, while the other is reserved for a bigger project.

I got a diddy little OReilly mySQL reference guide through from Amazon the other day, dead useful it is :)
 
W

wyrd_fish

Guest
i used a noname sql db for my AS level course work...
 
J

Jonty

Guest
Originally posted by wyrd_fish
I used a noname sql db for my AS level course work...
Really? Cool! Hope it didn't let you down :D

Kind Regards
 
W

wyrd_fish

Guest
the only thing that annoyed me was the monthly code thing, but I see wht thats nessaserry


I used as the 34sp phpmyadmin was using a port that school firewalled out... the buggers...
 
J

Jonty

Guest
Yeah, the save account feature is a sad but necessary step. It's like the BarrysWorld forums, there are 24,007 members at the time of posting, and yet only a fraction of these actually use their accounts, leaving the rest to use up otherwise valuable resources. Just to clarify, though, accounts currently only have to be saved every 90 days/3 months, and I think (but don't take this as the gospel truth) that you also get an email when the time draws near so you don't forget to save your account.

Kind Regards
 

Users who are viewing this thread

Top Bottom