0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Alex
  • 1

2 Answers2

0

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?

  • PHP Version 5.3.14, Linux x86_64 – Alex Oct 25 '12 at 19:47
  • In working mode I use crontab, so F5 is not a reason... $i is ascending normally, include() dosn't see changes in file (about 10% of times), but file_get_content() see all changes well – Alex Oct 25 '12 at 19:52
0

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

Community
  • 1
  • 1
goFrendiAsgard
  • 4,016
  • 8
  • 38
  • 64