tags:

views:

218

answers:

1

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

A: 

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.

Stephan202