1

I am using latest version of php.. and i stuck on this error

Warning: fwrite() expects parameter 1 to be resource, string given in c:\ this error shows me 6 times

Warning: fclose() expects parameter 1 to be resource, string given in // this error reapeat only one time.

I am trying to get last id of last line but i am facing that error.. here is my php code :

<?php
include_once('../../fucrenzione.php');
/*
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";*/
$filename ="data.txt" or die("Unable to open file!");
$line="";
fopen($filename, 'a+');
    for($i=0;$i<=5;$i++ ){

        $va=rand(1,20);
        $re= rand(2,50);
        $data = [
                'val1' => $va,
                'val2' => $re, 
                'body' => getToken(10),
                'Id'=> $i,
                'timestamp' => time()
                ];

     /* echo   "<pre>".*/$line = serialize($data);
     $line .= "\n";
    fwrite($filename, $line);

    }
fclose($filename);
?>

I tried to use also fputs() but i still get that error.

Phoenix
  • 467
  • 1
  • 11
  • 23
  • Read manuals once again. It's easy. There's even an example. – u_mulder Dec 09 '15 at 19:47
  • can not find.. i am learning php.. sir – Phoenix Dec 09 '15 at 19:48
  • Go to http://php.net - this is the official documentation, and it is pretty awesome most of the time. There is a search box which allows you to search for functions like "fwrite". Go to that page (or use the shortcut: type "http://php.net/fwrite" to go directly to the page, or get a search result page with matching, but different functions if you misspelled something). Use the power of the documentation! – Sven Dec 09 '15 at 19:53

1 Answers1

5

The error tells you the issue. fopen() returns a resource:

$handle = fopen($filename, 'a+');

Then fwrite() expects the first argument to be that resource:

fwrite($handle, $line);

Also, I think the or die("Unable to open file!"); would be better on the fopen() line rather than the assignment line.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • It is such an easy question, and you didn't bother copy&pasting the error message into a search engine and read the results - why should this not be downvoted? A downvote says: This question does not show any research effort... - which is in fact the case here. – Sven Dec 09 '15 at 19:55
  • i searched i couldnot find anything i did nt think about handle sry – Phoenix Dec 09 '15 at 19:56
  • can u tell me also how can i get last line ? – Phoenix Dec 09 '15 at 20:06