3

I have the following code:

    <?php
        $myfile = fopen("code2.css", "w") or die("Unable to open file!");
        $txt = "John Doe\n";
        fwrite($myfile, $txt);
        $txt = "Jane Doe\n";
        fwrite($myfile, $txt);
        fclose($myfile);
    ?>
?>

code2.css is in the same folder as my PHP file, but it throws me:

Unable to open file all the time.

How can I fix this?

Update: After playing with permissions the error dissapeared, but still my file won't be updated.

10
  • 2
    Maybe remove ?> at the end of your code? Commented Dec 16, 2014 at 13:18
  • 1
    Seems you have 1 ?> too many. Commented Dec 16, 2014 at 13:18
  • 1
    check file permissions. Commented Dec 16, 2014 at 13:20
  • @Fres-ii- how to do that in localhost Commented Dec 16, 2014 at 13:21
  • @stranger4js This should work: echo substr(sprintf('%o', fileperms('code2.css')), -4); Commented Dec 16, 2014 at 13:22

3 Answers 3

4

Check the properties of code2.css. You must found the "read only" permission to it, and change it to "Read and Write". After that your code will work.

If you are using a Linux system, then execute:

sudo chmod 777 code2.css
Sign up to request clarification or add additional context in comments.

4 Comments

And where is the solution so that OP's code work?! Here i only see a comment!
actually I used the same code just removed unnecessary ?> and it worked
Maybe for you, but OP clearly said in the comment's under his question that it didn't worked for him! (See: stackoverflow.com/questions/27505561/cant-open-file-php/…)
for your kind of information it didnot worked in my system too ... but what ever the procedure I have followed in my system, after that I got the two lines written in code2.css. This is the only reason why I have not put comment and posted separate answer
2
<?php  
    $myfile = fopen("code2.css", "w") or die("Unable to open file!");
    $txt = "John Doe\n";
    fwrite($myfile, $txt);
    $txt = "Jane Doe\n";
    fwrite($myfile, $txt);
    fclose($myfile);//just removed the closing php tag from here and it is working fine 
?>

4 Comments

OP already said that this made no difference for him!
@Rizier123 what does OP mean?
@stranger4js Original Poster :D That's you in this case (meta.stackexchange.com/q/146513)
@Rizier123 thanks :D I have seen that many times but LOL
2

fopen() returns false and generates an E_WARNING alert on fail.
You should begin with displaying all warnings, you'll get more information on the issue:

error_reporting(E_ALL);
ini_set('display_errors', '1');

Please post the warning. Probably rights on your file or folder. Please make sure your webserver has write rights on it. What system are you running your localhost on?

4 Comments

where do i put that?
@stranger4js Normally at the top of your file :D
I think that should be a comment and not an answer!
Being new to SO, i still have no comment right (or just on my own questions and answers), but apart from that, your own comment is probably founded...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.