Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP fwrite不';t使用附加模式更新文件_Php_Linux_File_Fopen_Fwrite - Fatal编程技术网

PHP fwrite不';t使用附加模式更新文件

PHP fwrite不';t使用附加模式更新文件,php,linux,file,fopen,fwrite,Php,Linux,File,Fopen,Fwrite,下面的代码正在运行,但不会更新它创建的文件的内容 我可以看到文件内容已更改(大小增加),但当我从服务器下载文件时,它是空的。 该文件的chmod为666,并且也是其父目录 它是一个运行Apache和PHP的linux服务器。 我还尝试使用fflush强制它刷新内容 <?php header("Location: http://www.example.com"); $handle = fopen("log.txt", "a"); foreach($_POST as $variable =&

下面的代码正在运行,但不会更新它创建的文件的内容

我可以看到文件内容已更改(大小增加),但当我从服务器下载文件时,它是空的。 该文件的chmod为666,并且也是其父目录

它是一个运行Apache和PHP的linux服务器。 我还尝试使用fflush强制它刷新内容

<?php

header("Location: http://www.example.com");
$handle = fopen("log.txt", "a");
foreach($_POST as $variable => $value) {
   fwrite($handle, $variable);
   fwrite($handle, '=');
   fwrite($handle, $value);
   fwrite($handle, '\r\n');
}

fwrite($handle, '\r\n');
fflush($handle);
fclose($handle);

?>

有什么问题


谢谢

我认为一个好的做法是检查一个文件是否可以用
写,然后通过检查
fopen
返回的值来打开它,顺便说一句,你的代码是正确的

试试这个:

$filename = "log.txt";
$mode = "a";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, $mode)) {
         echo "Cannot open file ($filename)";
         exit;
    }

    foreach($_POST as $variable => $value) {
       fwrite($handle, $variable);
       fwrite($handle, '=');
       fwrite($handle, $value);
       fwrite($handle, '\r\n');
    }

    fwrite($handle, '\r\n');
    fflush($handle);
    fclose($handle);
    echo "Content written to file ($filename)";

} else {
    echo "The file $filename is not writable";
}

为什么
标题
重定向在第一行?试着评论一下!