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 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式