无法从PHP打开文件

时间:2014-12-16 13:17:35

标签: php file php-5.5

我有以下代码:

    <?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与我的PHP文件位于同一文件夹中,但它引发了我:

  

无法一直打开文件。

我该如何解决这个问题?

更新:播放权限后,错误消失,但我的文件仍然不会更新。

3 个答案:

答案 0 :(得分: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 
?>

答案 1 :(得分:2)

检查code2.css的属性。你必须找到&#34;只读&#34;对它的许可,并将其更改为&#34; Read and Write&#34;。之后,您的代码将起作用。

如果您使用的是Linux系统,请执行:

sudo chmod 777 code2.css

答案 2 :(得分:2)

fopen()返回false 并在失败时生成E_WARNING警报。 您应该首先显示所有警告,您将获得有关该问题的更多信息:

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

请发布警告。 可能是您的文件或文件夹的权利。 请确保您的网络服务器具有写权限。您在哪个系统上运行本地主机?

相关问题