file_put_contents不在一个地方工作,但在代码中的其他地方工作

Im new to PHP, but used to Java. I have a file, dd-functions.php that contains a function (writeToLog). This function will create a variable with the log-statement, then write it to a file.

The strange thing is that this works perfectly when it is called from one php-file (file1.php), but when called from another php-file (file2.php), it does not write anything to the file. In both file1.php and file2.php, the functions.php is included once.

How is this even possible?

Function.php

/* Log for user */
function writeToLog($user, $analyseName, $remaining_downloads) {

$uid = $user->ID;

$userdata = get_userdata($uid);
if ($userdata->first_name) {
    $username = $userdata->first_name . ' ' . $userdata->last_name;
} else {
    $username = $userdata->user_login;
}
date_default_timezone_set('Europe/Oslo');
$date = date('d.m.Y H:i:s');
$s2member_access_level = get_user_field ("s2member_access_level", $uid);
$userLog = $uid . "\t" . $date . "\t" . $username . "\t" . $user->user_email . "\t" . $analyseName . "\t" . $remaining_downloads . "\t" . $s2member_access_level . "
";

file_put_contents('analyse-email-click-log.txt', $userLog , FILE_APPEND);
}

You can extend the file with a hierarchy or you only have to include this file with the include statement.

Hierarchy

<?php

    class ClassA extends ClassB {


    }

Include

log.php
<?php

$logA = 'mylogA';
$logB = 'otherlog';

?>

test.php
<?php


include 'log.php';

echo "Show my $logA"; 

?>

This may be because of your relative path to analyse-email-click-log.txt.

If you have three php files like this

/lib.php
/foo.php
/sub/bar.php

Let writeToLog reside in lib.php and include the /lib.php file from /foo.php and /sub/bar.php. If you run /foo.php, it will write to /analyse-email-click-log.txt but if you run /sub/bar.php it will write to /sub/analyse-email-click-log.txt, which may be protected by write permissions.