473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fwrite error when trying to create a file

KeredDrahcir
426 256MB
I'm getting this error:
Warning: fwrite(): supplied argument is not a valid stream resource in /mypath on line 1222

My code is this:
Expand|Select|Wrap|Line Numbers
  1. $File = "custom.css";
  2.      $Handle = fopen($File, 'w') or die("can't open file");
  3.       if (fwrite($Handle, $css_file) === FALSE)
  4.       {
  5.         echo "Cannot write to file ($File)";
  6.         exit;
  7.       }
The error text also appears below the error. The CHMOD on the directory is 777.

Am I overlooking somethng?
Feb 2 '10 #1
6 2085
Atli
5,058 Expert 4TB
Hey.

Are you sure those are the correct lines from the code?

I ask because the fopen function either returns a stream resource, which would not cause this error, or it returns FALSE, which in your code would trigger the or die command.
This error shouldn't be possible using that code.

To be sure, thought, try printing the contents of the $Handle before the fwrite just to see exactly what it is:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $File = "custom.css";
  3. $Handle = fopen($File, 'w') or die("can't open file");
  4.  
  5. var_dump($Handle); exit;
  6.  
  7. if (fwrite($Handle, $css_file) === FALSE)
  8. {
  9.     echo "Cannot write to file ($File)";
  10.     exit;
  11. }
  12. ?>
Feb 2 '10 #2
KeredDrahcir
426 256MB
I will try that out. Line 1222 is:
if (fwrite($Handle, $css_file) === FALSE)
I'll find out what's in the Handle.
Feb 2 '10 #3
KeredDrahcir
426 256MB
When using the var dump it gives me:
resource(53) of type (stream)
I'm afraid I don't understand what this means. It it supposed to show something else?
Feb 2 '10 #4
KeredDrahcir
426 256MB
It's working now. All I did was add the code you suggested, looked at the error message and took the code out and tired it again and it's working.
I'm glad it's working but I'd like to know why.

Thanks for your help.
Feb 2 '10 #5
Atli
5,058 Expert 4TB
Hmm... odd.

Did you copy the original code from somewhere? Like a web-page?
It is possible that there was a "special" character in there, invisible, which PHP took as a part of the variable name... Or just some odd charset mismatch.
Feb 2 '10 #6
KeredDrahcir
426 256MB
I can't remember. I think I copied the code from php.net. I would have thought there wouldn't be any invisible characters there.
The only think that worries me is that if it didn't work and them for seemingly no reason starts to work, I hope it doens't later on stop with no warning.

Thanks for all youe help.
Feb 9 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tuuska | last post by:
Hello! I have this problem, when fwrite() writes to the beginning of xyz.txt file, it overwrites the first line. Any ideas how to prevent this? I'm running PHP 4.3.3RC3 on Linux. <?php $fp =...
1
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
3
by: Bob Bedford | last post by:
I'm trying to save an XML file sent to me in ZIP format. Here is the code: if($XMLFile=fopen($XMLPath.strtoupper(substr($file,0,strlen($file)-4)).".XML",'w'))...
8
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same...
23
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
10
by: cs | last post by:
if i have typedef struct{ unsigned a; unsigned *b; }tp; i would like to ask if it is ok for the "C standard 89" doing something like this {
4
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
2
by: miraan | last post by:
Hi, I am trying to create a script that creates other php scripts but I am getting a problem, when I create the file and use fwrite() to write to it, it returns an error saying: Parse error: syntax...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.