- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的问题(可能不会出现在你的电脑上)
我有 2 个 PHP 脚本。
第一个脚本读取包括第二个脚本获取变量,更改值,并执行 file_put_contents 以更改第二个脚本。
<?php
include('second.php'); // in second.php, $num defined as "1"
$num ++; // now $num should be "2"
// Change content of second.php
file_put_contents('second.php', '<?php $num='.$num.'; ?>');
include('second.php'); // Now here is the problem, $num's value is still "1"
echo $num; // and I get an unexpected result "1"
?>
第二个脚本只包含一个变量
<?php $num=1; ?>
我希望结果是“2”,但似乎第二个包含没有读取 file_put_contents 所做的更改。
我的第一个猜测是 file_put_contents 函数中可能存在并发问题,因此在执行第二个 include 时第二个文件并没有真正改变。
我尝试通过将第一个脚本更改为以下内容来验证我的猜测:
<?php
include('second.php');
$num ++;
file_put_contents('second.php', '<?php $num='.$num.'; ?>');
// show the contains of second.php
echo '<pre>' . str_replace(array('<','>'), array('<', '>'),
file_get_contents('second.php')) . '</pre>';
include('second.php');
echo $num;
?>
我真的很惊讶地发现程序的结果是这样的:
<?php $num=4; ?>
3
这意味着 file_put_contents 正确读取了文件(换句话说,文件确实发生了物理更改),但“include”仍然使用第一个值。
我的问题
我已经阅读了这个问题,但没有找到答案:
Dynamically changed files in PHP. Changes sometimes are not visible in include(), ftp_put()
临时解决方法
使用 eval 似乎是临时的解决方法。这并不优雅,因为 eval 通常与安全漏洞相关联。
<?php
require('second.php');
$num ++;
file_put_contents('second.php', '<?php $num='.$num.'; ?>');
echo '<pre>' . str_replace(array('<','>'), array('<', '>'), file_get_contents('second.php')) . '</pre>';
require('file.php');
echo $num . '<br />';
eval(str_replace(array('<?php','?>'), array('', ''), file_get_contents('second.php')));
echo $num;
?>
这是结果:
<?php $num=10; ?>
9
10
最佳答案
可能您已经安装并启用了 OPcache(自 Php 5.5: OPcache extension added 起),正在缓存您的 second.php
文件?
查看phpinfo()
是否为真。
如果是这样,使用 opcache_invalidate('second.php')
使缓存文件无效或使用 opcache_reset()
重置所有缓存文件。
<?php
include('second.php');
$num ++;
file_put_contents('second.php', '<?php $num='.$num.'; ?>');
opcache_invalidate('second.php');//Reset file cache
include('second.php');
echo $num;//2
?>
关于PHP include 不读取源文件的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24074220/
我有一个项目,其中包含两个源文件和一个头文件(其中包含名为 Get.c 的源文件之一中定义的函数的原型(prototype)),问题是: 当我在另一个源文件中包含 Get.c 时,它给出了错误 错误:
有没有尽可能多的语法结构的java源代码文件? java 编译器的某种测试文件。我需要该文件来测试 java 源代码解析器。 最佳答案 看看 Java Compatibility Kit ,特别Tec
尝试在命令提示符下编译时,我在使用 codeblocks-16.01mingw-setup.exe - 安装在路径不包含空格的文件中时遇到问题。初学者指南建议在命令提示符中使用以下行: gcc car
我编写了一个使用 Cryptopp 库的程序。我已经创建了静态库并将其包含在我的分发版中,但是该程序还需要很多头文件。我的问题是我需要在我的发行版中包含所有这些头文件吗? 例如,我编写了一个代码,其中
我在源代码树中有 protobuf 原型(prototype)文件。我想在每次更改和首次运行时从原型(prototype)文件生成源文件(例如,我创建新的原型(prototype)文件)。然后,我想从
我知道在 Java 中加载文件而不指定要使用的编码是平台相关的。但我的问题是关于 .java 源文件本身中包含的文本:用于这些文件的编码是否仍然相关一旦编译? 例如,如果我在 Windows 上有一个
编辑:{ 我想我在这里添加了很多(太多)信息(阅读)。我想要的是: 我正在使用一个网站(没有.csproj文件) 我需要多个源代码文件才能运行我的aspx 据我所知,当请求aspx文件时,我要么需要使
我从 Asio 的示例页面中获取了以下代码 class tcp_connection : public boost::enable_shared_from_this { public: t
我已经使用 VS2010 为我的 C++ 应用程序创建了一个 Windows 安装部署。但是我的问题是我不知道如何将我自己的一些代码压缩到安装向导中(这可能吗?)。问题是在安装过程中我想要: 要求用户
最近我创建了一个 bash 脚本,我应该在 cron 中运行它。 准备好bash脚本并正常运行后,我将其放入Cron中,发现它失败了。作为第二步,我删除了所有环境依赖项,即我指定的不是 file.tx
考虑以下场景 例子.txt: ÄäÖöÜü Java 源代码: try (FileInputStream fileInputStream = new FileInputStream("example.
背景我主要使用嵌入式 C/C++ 进行编程,但有时我必须为我们的 API 进行一些 C# 编程。为此,我使用 Visual Studio 2017 为我们的客户创建 API DLL。 C# API 和
这个问题在这里已经有了答案: ggplot's qplot does not execute on sourcing (1 个回答) 关闭 9 年前。 考虑这种形式的源文件: # initializ
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我需要读取一个 Racket 源文件并通过宏扩展来运行它。我有一个简单的测试文件,Racket 本身很乐意接受: C:\ayane>type factorial.rkt #lang racket (p
我的源文件位于此文件夹中:c:\data\mycompany。我的所有源文件的第一行都包含以下内容:package mycompany;现在,我从 c:\data 文件夹中使用以下命令编译了所有内容:
这个问题在这里已经有了答案: #include all .cpp files into a single compilation unit? (6 个回答) The benefits / disadv
我对这个 JAVA RMI 项目真的很陌生。我只是想知道我可以使用什么编译器来编译我的JAVA源代码?以及如何做到这一点? 我正在尝试编译来自 http://www.eg.bucknell.edu/~
我想使用 Subversion 并仅 check out 源文件(例如:仅 check out .c 、 .cpp 和 .h 文件)。这可能吗 ?如果是这样,我该怎么做? 我正在尝试从以下位置获取 w
有没有办法使用指向唯一头文件的不同配置(或不同方法)来设置单个项目。 我想创建一个包含两个头文件的 C 项目(例如 header1.h 和 header2.h)。在我的 .c 源文件中,我定义了两个配
我是一名优秀的程序员,十分优秀!