PHP文件附加问题

时间:2017-03-20 22:43:17

标签: php append

我编写了一个旨在修改文本文件的脚本,但是在将文本写入文件时存在问题。 该脚本看起来有点不正统,因为我编辑了几次尝试找到解决方案。问题在于实际的附加过程,我收到通过脚本的echo部分输入的文本,这不是问题。

<?php
$text=$_POST['typer'];
echo $text;
function write(){
    global $text;
    $myfile = fopen('text.txt','a');
    fwrite($myfile, $text);
    fclose($myfile);
}
write()
?>

2 个答案:

答案 0 :(得分:-1)

您可以将$ text作为参数传递。不需要全局

<?php
$text=$_POST['typer'];
echo $text;
write($text);

function write( $text ) {
    $myfile = fopen('text.txt','a');
    fwrite($myfile, $text);
    fclose($myfile);
}
?>

要使其适用于全局变量,您需要$GLOBALS["text"]代替global $text

答案 1 :(得分:-1)

这应该有效:

<?php
$text=$_POST['typer'];
echo $text;
function write($text) {
    $myfile = fopen('text.txt','a');
    fwrite($myfile, $text);
    fclose($myfile);
}
write($text);
?>

否则配置错误记录并查看是否还有其他事情发生......

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
    $text=$_POST['typer'];
    echo $text;
    function write($text) {
        $myfile = fopen('text.txt','a');
        fwrite($myfile, $text);
        fclose($myfile);
    }
    write($text);
    ?>

根据我的经验,它通常是权限问题,而您尝试写入的文件没有必要的权限来允许您打开和写入它。