Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't open file from PHP [closed]

Tags:

file

php

php-5.5

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.

like image 321
stranger4js Avatar asked Jan 24 '26 04:01

stranger4js


2 Answers

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
like image 107
Neo Avatar answered Jan 25 '26 18:01

Neo


<?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 
?>
like image 35
Davan Polampalli Avatar answered Jan 25 '26 16:01

Davan Polampalli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!