PHP4 在 fwrite() 创建的文件中使用 include() 时出现问题
我有一个名为 generator.php
的文件,它使用 fwrite()
在服务器(Apache、PHP4)上创建 result.php
。
result.php
中的一行是 PHP include()
语句。
因此,在 generator.php
中:
if (!is_file($fname)){
$resultfile = fopen($current_path . "/" . $fname, "w+");
}
fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "'. '/inc/footer.php"); ?>' . "\n");
fclose($resultfile);
chmod($current_path . "/" . $fname, 0755);
在 result.php
中:
<h2>Sponsored Links</h2>
<!-- begin sidebar_top ad -->
<?php echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php"); ?>
<!-- end sidebar_top ad -->
但是当我访问 时,
include()
语句不起作用result.php 在浏览器中。 echo 语句确实如此,所以我知道路径是正确的。
另一个具有相同代码的 test.php
,我使用 FTP 上传到同一文件夹中,工作正常。
通过 FTP 恢复时,两个文件中的代码相同。
在 test.php
中:(正确地工作、回显和包含。)
<?php
echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php");
?>
知道为什么 include()
在 test.php
中工作(创建手动)而不是在 result.php
(使用 fwrite()
创建)中,当两者都在同一个文件夹中时?
我知道这些文件之间的唯一区别
- 是:所有者可能不同(
result.php
不会由用户nobody
创建吗?) - 权限最初是不同的。 FTP 文件(工作)为
0775
,而使用 fwrite() 创建的文件(包括不工作)则为664
,并由生成器进行 chmoded。 php
到0775
。 - 工作
test.php
文件是在 Mac 上使用 Smultron 进行编辑并通过 FTP 上传的,而result.php
是通过fwrite()
在 < Linux 上的 code>generator.php,从浏览器调用。
I have a file called generator.php
that uses fwrite()
to create a result.php
on the server (Apache, PHP4).
One of the lines in result.php
is a PHP include()
statement.
So, in generator.php
:
if (!is_file($fname)){
$resultfile = fopen($current_path . "/" . $fname, "w+");
}
fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "'. '/inc/footer.php"); ?>' . "\n");
fclose($resultfile);
chmod($current_path . "/" . $fname, 0755);
And in result.php
:
<h2>Sponsored Links</h2>
<!-- begin sidebar_top ad -->
<?php echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php"); ?>
<!-- end sidebar_top ad -->
But that include()
statement doesn't work when I visit result.php
in a browser. The echo statement does, so I know the path is correct.
Another test.php
with the same code, which I uploaded using FTP into the same folder, works fine.
The code in the same in both files, when recovered via FTP.
In test.php
: (works, echoes and includes correctly.)
<?php
echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php");
?>
Any idea why the include()
is working in test.php
(created manually) and not in result.php
(created using fwrite()
), when both are in the same folder?
The only differences I know of between the files:
- Owner could be different (wouldn't
result.php
be created by usernobody
?) - Permissions are originally different. FTP'd file (working) is
0775
, while the ones created using fwrite() (include not working) had664
, and is chmoded by thegenerator.php
to0775
. - Working
test.php
file was edited on a Mac with Smultron and uploaded via FTP, whileresult.php
was created byfwrite()
ingenerator.php
on Linux, called from a browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你那里有一个额外的“
you had an extra " in there i think
当 PHP4 安全模式打开时,由另一个 uid 写入的
result.php
无法访问属于另一个 uid 的包含文件。我通过打开
php.ini
并更改为safe_mode_gid = On
解决了这个问题,并将我的包含目录添加到safe_mode_include_dir
。我还必须重新启动 Apache 才能使更改生效。
When PHP4 safe mode is on, the
result.php
, being written by another uid, cannot not access the included file, which belongs to another uid.I resolved this by opening
php.ini
and changing tosafe_mode_gid = On
, and adding my includes directory tosafe_mode_include_dir
.I also had to restart Apache to let the changes take effect.