开发者

How to map file path from different folder location easily?

how to map the path to the file easily?

public_html/api/function.php
<?php

function writetologfile($content)
{

    $filename = 'logfile/testing_randomstring.txt';

    if (!$handle = fopen($filename, 'a')) 
    {
        echo "Cannot open file ($filename)";
        exit;
    }
    fclose($handle);    
}

?>

the actual path of the text file is in public_html/开发者_运维技巧r/admin/logfile/testing_randomstring.txt

so if I run the script at public_html/folder1/folder2/addlog.php, it won't be able to find the path to the testing_randomstring.txt

addlog.php
<?php

include("../../api/function.php");

writetologfile('hahaha');

?>

How I can able to easily point to this text file path, no matter where my php calling script is from.

I tried to change $filename = 'logfile/testing_randomstring.txt'; inside writetologfile function by enforcing it to absolute fix path, something like $filename='/r/admin/logfile/testing_randomstring.txt', but it is not working


Instead of using a relative path, you could specify an absolute path. Assuming public_html is in your home directory, try this:

$filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
fopen(getenv('HOME') . $filename, 'a');

This uses getenv to read the contents of the environment variable $HOME.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜