-3

I'm pretty new at using fwrite and file_put_contents and am getting errors on every line of my for loop statement:

Warning: file_put_contents() expects parameter 1 to be string,

or

Warning: fwrite(): 3 is not a valid stream resource in...

This is the code:

$databank = "data.txt"; $access = fopen($databank, 'w')or die("cant open file"); fclose($access);

$row = "$ent1".' | '."$ent2".' | '."$perc1a".'% | '."$perc1a_frac".'% | '."$frac1a".' | '."$perc2a".'% | '."$perc2a_frac".'%  | '."$frac2a".' | +'."$a".' | '."$new_ent1".' | '."$perc1b".'% | '."$perc1b_frac".'% | '."$frac1b".' | '."$perc2b".'% | '."$perc2b_frac".'% | '."$frac2b".' \n';
            fwrite($access, $row);
            //file_put_contents($access,$row);

I have a hunch its a string related issue. Any pointers in greatly appreciated.

Joe Shamuraq
  • 1,245
  • 3
  • 18
  • 32
  • 2
    First of all I'd say you should understand those error messages. Not just replace the function name with another one as a form of guessing. The [error reference](http://stackoverflow.com/q/12769982/367456) might be helpful for that. – hakre Jan 20 '13 at 09:14
  • what's in $access variable for file_put_contents ...? should be the name of file to which you are putting the contents...! – Sudhir Bastakoti Jan 20 '13 at 09:16
  • @Sudhir: That is part of the error message. It's the number 3. – hakre Jan 20 '13 at 09:18
  • i've added to show where $access is declared... – Joe Shamuraq Jan 20 '13 at 10:31

4 Answers4

1

#1. Error:

Warning: file_put_contents() expects parameter 1 to be string

In documentation, it say's that first parameter is string $filename and explanation for that: Path to the file where to write the data..

Example of use:

$file = 'people.txt';
$current = "John Smith\n";
file_put_contents($file, $current);

#2. Error:

Warning: fwrite(): 3 is not a valid stream resource in...

Again, in documentation it say's that the first parameter is resource $handle and wxplanation for that: A file system pointer resource that is typically created using fopen()..

Example of use:

$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
Glavić
  • 42,781
  • 13
  • 77
  • 107
1

using file_put_contents(), you should have following:

$access -- should be a filename, and since as per your question you are using it inside loop, so you should use FILE_APPEND in order to append the new content, like:

$access = "some_filename.txt";
file_put_contents($access, $yourDataHere, FILE_APPEND | LOCK_EX);
//LOCK_EX prevents anyone else writing to the file at the same time

Better, read the docs for the function you are going to use.

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

It is extremely good practice to read a manual page before using some function. I myself always follow it, no exceptions.

Say, from one for file_put_contents you can learn 2 things:

  1. $access should be a filename.
  2. to make it write many lines in a loop, special flag have to be used or file will be constantly overwritten with the last line
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

I actually overlooked the

fclose($access);

I placed it before the fwrite() when it should be after. Its all ok now.

Joe Shamuraq
  • 1,245
  • 3
  • 18
  • 32