【问题标题】:Using php creating file and adding text to it使用 php 创建文件并向其添加文本
【发布时间】:2013-09-05 17:50:40
【问题描述】:

我正在尝试使用 php 创建一个文本文件,并且该值来自一个 db 调用

$result = mysql_query("SELECT code FROM topic_master");
while ($row = mysql_fetch_array($result)) {
$x1=$row{'code'};
exec("printf $x1 >> code.txt");
}

但是该值没有插入到 code.txt 中。只有数组(代码)被插入到 code.txt 中。 实际上 $row{'code'} 有“#xy { } () %”。如何将值写入 txt 文件。

【问题讨论】:

    标签: php linux shell


    【解决方案1】:

    这将解决您的问题:

    $result = mysql_query("SELECT code FROM topic_master");
    while ($row = mysql_fetch_array($result)) {
    file_put_contents('code.txt',$row['code'],FILE_APPEND);//FILE_APPEND is necessary
    }
    

    【讨论】:

    • 每次刷新都会追加相同的数据。
    • @Salim no ,每次迭代中数据都会改变,并且 File_APPEND 将光标放在文件末尾,因此数据不会被删除
    • $x1="abc"; file_put_contents('code.txt',$x1, FILE_APPEND);如果您刷新页面 3 次输出将是 'abc abc abc'
    • @Salim OP 对此没有说什么。他应该指定他想要什么,不要忘记数据库不是变量并且值会改变
    • 每个周期都不需要$x1=$row['code'];。只是file_put_contents('code.txt', $row['code'], FILE_APPEND);。不推荐使用 Mysql。循环文件访问而不是一次保存...
    【解决方案2】:

    使用 file_put_contents 代替那个 exec 并执行

    $s = "";
    while ($row = mysql_fetch_array($result)) {
        $x1=$row['code'];
        $s .= $x1 . "\n";
    }
    file_put_contents("code.txt", $s, FILE_APPEND);
    

    【讨论】:

    • 使用FILE_APPEND 标志不擦除以前的数据
    • 我觉得 \r\n 更好
    • @FaceOfJock 在 Linux 中使用 \r\n 会产生不良结果。这适用于 Windows,但不适用于 Linux。
    • @Fred-ii- 但在 notepad++ 中,\n 不会传递到下一行
    • @FaceOfJock 你怎么看“不会传递到下一行”
    【解决方案3】:

    首先,你不应该使用 $row{'code'} 正确的用法是 $row['code']

    另外,我建议使用 file_put_contents() 而不是 exec 这样你就可以这样做

    $result = mysql_query("SELECT code FROM topic_master");
    while ($row = mysql_fetch_array($result)) {
    $x1=$row['code'];
    file_put_contents('code.txt',$x1, FILE_APPEND);
    }
    

    【讨论】:

    • FILE_APPEND 应该在 file_input 中,否则内容将被删除,只有最后一行会在文件中
    • 我认为每次刷新此页面都会附加数据。每次都会存储相同的数据
    • $x1="abc"; file_put_contents('code.txt',$x1, FILE_APPEND);如果您刷新第 3 页时间输出将是 'abc abc abc' 我认为他不想要这个
    • 查看答案 code.txt 将包含每次刷新时数据库中的数据量stackoverflow.com/a/18642996/2459296
    【解决方案4】:

    实际上,您不是使用 PHP 创建文本文件,而是使用 shell 执行创建文本文件。为什么不试试文件函数呢?

    $result = mysql_query("SELECT code FROM topic_master");
    $file = fopen("code.txt", "ab");
    while ($row = mysql_fetch_array($result)) {
        fwrite($file, $row["code"]);
    }
    fclose($file);
    

    【讨论】:

    • "ab"? b 用于二进制。 aa+ 如果是文本,则不是 b 开关。
    • PHP manual 说:“为了可移植性,强烈建议您在使用 fopen() 打开文件时始终使用 'b' 标志。”
    【解决方案5】:

    获取变量中的所有数据并写入文件

    <?php 
    
    $result = mysql_query("SELECT code FROM topic_master");
    
    $data = "";
    while ($row = mysql_fetch_array($result)) {
    
        $x1=$row['code'];
    
        $data .= $x1;
    
    }
    
    $fp = fopen("code.txt","w");
    
    fwrite($fp,$data);
    
    fclose($fp);
    
    ?> 
    

    【讨论】:

    • fwrite($fp,$data) 而不是 fwrite($fp,$x1) 以及 $fp = fopen("code.txt","a"); 将指针放在文件末尾而不是开头
    • @Salim w 将覆盖内容。 OP需要追加,因此aa+
    • 它在循环之外,所有值都存储在变量 $data 中,它只写入文件一次
    • @Salim 根据 OP 的标题 “使用 php 创建文件并向其中添加文本” 查看接受的答案,其中使用了FILE_APPEND
    • 读取例外答案中的 cmets
    【解决方案6】:

    $result = mysql_query("SELECT code FROM topic_master");
    
    // if you want to APPEND the data in code.txt, use this
    // however, using that particular SQL query the same data will be written over and over because there's nothing specifying a parameter change
    while ($row = mysql_fetch_array($result)) {
        $x1 = $row['code'];
        write_file('code.txt',$x1,'a');
    }
    
    // if you want to OVERWRITE the data in code.txt use this
    while ($row = mysql_fetch_array($result)) {
        $x1 .= $row['code'];
    }
    write_file('code.txt',$x1,'w');
    
    
    // function to write content to a file (with error checking)
    function write_file($filename,$content,$flag) {
        if(is_writable($filename)) {
            if(!$handle = fopen($filename, $flag)) {
                // die, or (preferably) write to error handler function
                die('error');
            }
            if(fwrite($handle, $content) === FALSE) {
                // die, or (preferably) write to error handler function
                die('error');
            }
            fclose($handle);
        }
    }
    

    编辑:将标志从 w 更改为 a。

    另一个编辑:如果您想附加到文件,请将 fopen() 标志设置为“a”。如果要覆盖现有内容,请将标志更改为“w”。

    最终编辑:添加了两个带有解释的版本

    【讨论】:

    • w 将覆盖内容。 OP需要追加,因此aa+
    • 谢谢,没看到。他在哪里声明他需要追加?
    • 不客气,蒂姆。在标题中“使用php创建文件并添加文本”
    • 是的,重要的东西在标题而不是问题本身时并不总是很清楚;一个诚实的错误;-)
    • 无论如何都令人困惑,因为“添加”可能意味着两者。 :)
    猜你喜欢
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多