fopen不写入文件?

时间:2013-10-15 21:12:12

标签: php permissions fopen

error_reporting(E_ALL); 
ini_set('display_errors','1'); 
if (file_exists('/../_config.php')) { 
    $f = fopen('/../_config.php', 'w') or die($php_errormsg);
    fwrite($f, '<?php');
    fclose($f);
}
else {
    echo 'file doesnt exist';
}

返回: Nothing

我检查了_config.php文件,它是空的。 它应该包含<?php

绝对没有错误&amp;代码根本不会死。

这是与权限相关的问题吗?我在Windows 7上。

2 个答案:

答案 0 :(得分:1)

使用正确的文件路径:

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

$configFile = __DIR__ . '/../config.php';

echo $configFile, "\n";

if (file_exists($configFile)) { 
    $f = fopen($configFile, 'w') or die('cannot open file');
    fwrite($f, '<?php');
    fclose($f);
}
else {
    echo 'file doesnt exist', "\n";
}

您可以使用__DIR__代替dirname(__FILE__),但这仅适用于过时的(死的)PHP版本。

答案 1 :(得分:0)

检查您的php文件权限(将其设置为666),并检查目录的权限,以确保将其设置为777

相关问题