dongyi2083 2015-05-21 17:50
浏览 39
已采纳

Php将字符串写入../src中的文件

So I'm trying to get my php script append a text to log.txt, log.txt is placed in src/log.txt and the php script in web/script.php (they have other names but well...) So I've tried:

$logfile1 = fopen('log.txt', 'a', [$use_include_path = TRUE]);
$towrite = date('l jS \of F Y h:i:s A').': '.$var1.' did '.$var2.' and '.$var3.'
';
fwrite($logfile1, $towrite);

Wich didn't work... (i can echo $towrite and get a valid result) Note that in the top of the file

set_include_path("../src/" . PATH_SEPARATOR . get_include_path());

is done.

So... I continued with trying:

file_put_contents('log.txt', $towrite, FILE_APPEND | FILE_USE_INCLUDE_PATH);

Instead of fwrite... It didnt work either... Even trying to put log.txt in web/ and having

file_put_contents('log.txt', $towrite, FILE_APPEND);

So now I'm turning to the masters ;) how do I write that string to the file src/log.txt from web/script.php?

The file tree:

| - web
| | - script.php
| - src
| | - log.php
  • 写回答

1条回答 默认 最新

  • dqan70724 2015-05-21 18:00
    关注

    You may indeed use absolute paths here.

    Say your file structure looks something like this:

    |- src
    |  |- log.txt
    |-source
       |- code.php
    

    Then we'd perform the following code to go one dir up, ignoring any include_dir setting currently active.

    <?php
    $log = dirname(__FILE__) . "/../src/log.txt";
    
    $date = date('l jS \of F Y h:i:s A');
    $write = "{$date}: {$var1} did {$var2} and {$var3}
    ";
    
    file_put_contents($log, $write, FILE_APPEND);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导