为什么包含空文件会导致PHP脚本失败?

时间:2013-01-03 05:54:18

标签: php

如果第6行(.include ...)被取消注释,则以下脚本失败 - 无论文件“somefile.php”的内容是什么。即使是0字节(空)文件。我试图移动这些函数并创建一个“utils.php”库,这是PHP世界中的一个大禁忌吗? Error_log为空。我在Linux 2.6.32上使用PHP 5.2.17。谢谢。

<?php // File: index.php
error_reporting (E_ALL);
ini_set ('display_errors', 1);

//Uncomment the following line and the script fails
//.include "somefile.php";

function imageToBrowser ($pseudonym, $filePath) {
        header("Pragma: public");
        header("Expires: 0");
        header('Cache-Control: no-store, no-cache, must-revalidate');
        header('Cache-Control: pre-check=0, post-check=0, max-age=0', false);
        header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
        header("Content-Length: ".(string)(filesize($filePath)));
        header('Content-Type: application/x-download');
        header('Content-Disposition: attachment; filename="'.$pseudonym.'.png"');
        header('Content-Transfer-Encoding: binary');

        if ($file = fopen($filePath, 'rb')) {
                while(!feof($file) and (connection_status()==0)) {
                        print(fread($file, filesize($filePath)));
                        flush();
                }
                fclose($file);
}}

if (isset($_GET['showImage']) && ($imageMask = $_GET['showImage'])) {
        imageToBrowser ($imageMask, 'Clear64.png'); // Use any .png file in $cwd
} else {
        echo "<html><body><center>\n";
        echo "<img border='0' src=\"{$_SERVER['PHP_SELF']}?showImage=365\" alt=\"Missing Image\"/>\n";
        echo "</center></body></html>";
}
?>

2 个答案:

答案 0 :(得分:1)

在包含之前应该没有.。 Dot是PHP中的连接运算符,与此无关。

另请注意,由于显而易见的原因,您的ini_set ('display_errors', 1);语句无法解析解析错误。最好在php.ini或web-server config中设置您的设置。

答案 1 :(得分:0)

首先在include语句

之前删除dot

require()函数与include()相同,只是它以不同方式处理错误。如果发生错误,include()函数会生成警告,但脚本将继续执行。 require()生成致命错误,脚本将停止。

类似地,require_once()语句与require()相同,除了PHP将检查文件是否已被包含,如果是,则不再包括(require)它。

所以请你使用

更好
require_once('somefile.php');