在PHP中动态更改文件。有时在include(),ftp_put()中不可见 [英] Dynamically changed files in PHP. Changes sometimes are not visible in include(), ftp_put()

查看:142
本文介绍了在PHP中动态更改文件。有时在include(),ftp_put()中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的脚本:

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

或:

  $ xml ='<?xml version =1.0encoding =UTF-8?>'。\\\
< xml> \\\
\t< items> \\\
。$ xml_0 \\\
\t< /项目> \\\
< / 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);

实际上对这些文件的更改是物理应用的(写入文件)。

但是在include()之后有时不可见,或者没有通过ftp_put()发送到远程服务器。

似乎像PHP缓存文件。

在include()之前加入sleep(1)并没有帮助。

A也有一个($ i = 1; $ i <= 100; $ i ++){


$ p $ 回声$我);
$ filter_arr = array($ i);
file_put_contents(test.txt,'< ;? $ filter_arr ='.var_export($ filter_arr,true)。';?>');
include(test.txt);
echo $ filter_arr [0]。< br>;



$ b $大概90%的时间输出是正常的:

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


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

玩flock()或clearstatcache()也没有任何影响。

解决方案

问题。

编辑正确答案

您可以使用


$ b $ pre $ opcache_invalidate('second.php'); //重置文件缓存

如下所述: PHP包括不读取源文件的变化


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.

解决方案

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

这篇关于在PHP中动态更改文件。有时在include(),ftp_put()中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆