0

i have an few error in calling php file the error is

Warning: fopen(beasweb/1qHdAfvx1GQPUzef4BGiEg$21J.in): failed to open stream: No such file or directory in C:\xampp\htdocs\beasweb\index.php on line 140

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\beasweb\index.php on line 142

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\beasweb\index.php on line 143

and the line 140, 142, 143 is

$fh = fopen($filename, 'a');                                ///140
$filestring=chr(239).chr(187).chr(191).$filestring;
fwrite($fh, $filestring);                                   ///142
fclose($fh);                                                ///143

whats the problem in my code? anyone can solve my problem? thanks

Derry Susilo
  • 35
  • 2
  • 9
  • The first error is because a file with that name doesn't exist. The function returns a boolean type value, false, and this is stored in the variable $fh (file handle). The fwrite() and fclose() functions are expecting resource type parameters, though, not booleans, hence the second and third errors. – Luke Sep 09 '15 at 05:19

2 Answers2

1

You are getting error since you are using a mode you must create file if it does not exist. or use a+ mode which creates in case file does not exist.

so use fopen($filename, 'a+');

a: Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.

Quoting from php manual

a+ Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

Your code was....

$fh = fopen($filename, 'a');                                ///140
$filestring=chr(239).chr(187).chr(191).$filestring;
fwrite($fh, $filestring);                                   ///142
fclose($fh);                                                ///143

My code is ....

$filename ="hello.xls";
$fh = fopen($filename, 'a');  
 $filestring=chr(239).chr(187).chr(191).$filename;
fwrite($fh, $filestring);   
fclose($fh); 

when run this code then create one excel file (You can give any format like .txt, .php etc) and same texts insert within the file as file name....

CodeLove
  • 462
  • 4
  • 14
  • *Meta:* I've seen your recent [edit suggestions](http://stackoverflow.com/users/3688102/codelove?tab=activity&sort=suggestions) and I have to say that you're doing a lot of unnecessary and wrong edits (please review your rejected edits). Mainly, please don't use inline code formatting for non-code related stuff such as products, names, technologies, etc. It's fine to use inline code formatting for variables, but it's not a tool for general emphasis. That's what `*` and `**` are for. – Artjom B. Oct 27 '15 at 12:04