Dynamically changed files in PHP. Changes sometimes are not visible in include(), ftp_put()

[亡魂溺海] 提交于 2019-12-25 03:24:08

问题


I have scripts like these:

file_put_contents("filters.php", '<? $filter_arr = '.var_export($filter_arr, true).'; ?>');
include("filters.php");

or:

$xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n<xml>\n\t<items>\n".$xml_0."\n\t</items>\n</xml>";
file_put_contents($PROJECT_ROOT."/xml/$file_type.xml", $xml);
$upload_result = ftp_put($ftp_stream, $destination_file, $PROJECT_ROOT."/xml/$file_type.xml", FTP_BINARY);

Actually changes to those files are applied physically (written to files).

But sometimes not visible after include(), or not sent by ftp_put() to remote server.

It's seems something like PHP caching this files.

Adding sleep(1) before include() doesn't help.

A also have a test like this:

for ($i=1; $i <= 100; $i++) {
    echo "$i)";
    $filter_arr = array($i);
    file_put_contents("test.txt", '<? $filter_arr = '.var_export($filter_arr, true).'; ?>');
    include("test.txt");
    echo $filter_arr[0]."<br>";
}

About 90% of times output is normal:

1) 1
2) 2
...
100) 100

About 10% of times output is wrong:

1) 1
2) 1
...
100) 1

Playing with flock() or clearstatcache() also have no affect.


回答1:


It seems no filesystem or file locking problem, because in both times file is written, but one time with incorrect data, like $i is not ascending, what is weird. Only error that i got was that the file was locked for writing when i hold down F5, thats it.

Can you be more exact with versions and OS?




回答2:


I have face the same problem.

EDIT The correct answer

You can use

opcache_invalidate('second.php');//Reset file cache

as stated here: PHP include doesn't read changes of source file



来源:https://stackoverflow.com/questions/13075449/dynamically-changed-files-in-php-changes-sometimes-are-not-visible-in-include

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!