PHP4 problems with include() within a file created by fwrite()
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.phpbe 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.phpto0775. - Working
test.phpfile was edited on a Mac with Smultron and uploaded via FTP, whileresult.phpwas created byfwrite()ingenerator.phpon Linux, called from a browser.
fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php"); ?>' . "\n");
you had an extra " in there i think
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.
SAFE MODE Restriction in effect. The script whose uid is 48 is not allowed to access /var/www/vhosts/example.com/httpdocs/ads/sidebar_top.php owned by uid 10010 in /var/www/vhosts/example.com/httpdocs/results/result.php on line 130
I resolved this by opening php.ini and changing to safe_mode_gid = On, and adding my includes directory to safe_mode_include_dir.
I also had to restart Apache to let the changes take effect.
加载中,请稍侯......
精彩评论