代码之家  ›  专栏  ›  技术社区  ›  Marci-man

使用PHP将文本文件写入同一服务器的特定URL

  •  1
  • Marci-man  · 技术社区  · 14 年前

    我有一个php脚本,它将文本文件写入脚本所在的同一目录。

    提供的位置为URL格式: "www.example.com/main/foo/"

    有没有办法将文本文件写入这个特定的文件夹?我是在同一台机器上写的,所以我想应该不会很难吧? 谢谢你的帮助 干杯


    Flexi Comment Box

    3 回复  |  直到 14 年前
        1
  •  1
  •   mariana    14 年前

    我认为用最基本的fopen功能进行矫正是最好的方法。而不是使用绝对路径,你应该选择相对路径。
    语法如下:

    $fp = fopen('data.txt', 'a');
    

        2
  •  1
  •   Gabriel Solomon    14 年前

    file_put_contents

    <?php
    $file = 'people.txt';
    // The new person to add to the file
    $person = "John Smith\n";
    // Append the contents of $person to the file named by $file.
    file_put_contents($file, $person, FILE_APPEND);
    ?>
    
        3
  •  0
  •   Mark Baker    14 年前

    最好使用绝对磁盘引用而不是URL来写。从URL获取目录路径,标识到web根目录的绝对路径,并使用该路径为自己提供完整的文件路径。