Php 无法显示图像,因为它包含错误(帮助) 用户照片:

Php 无法显示图像,因为它包含错误(帮助) 用户照片:,php,Php,这段代码来自“PHP for Absolute初学者(2009).pdf”一书,但当我测试它时,我在Firefox中发现了以下错误: 无法显示图像,因为它包含错误 我能做些什么来修复它?您对imagejpeg的调用试图将图像写入一个具有空路径(第二个参数)的文件,而不是将其显示在浏览器中,因此您需要将此参数改为“null” 您需要将代码更改为: <?php // Checks if the form was submitted if ($_SERVER ['REQUEST_METHOD']

这段代码来自“PHP for Absolute初学者(2009).pdf”一书,但当我测试它时,我在Firefox中发现了以下错误:

无法显示图像,因为它包含错误


我能做些什么来修复它?

您对
imagejpeg
的调用试图将图像写入一个具有空路径(第二个参数)的文件,而不是将其显示在浏览器中,因此您需要将此参数改为“null”

您需要将代码更改为:

<?php
// Checks if the form was submitted
if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
// Checks if a file was uploaded without errors
if (isset($_FILES['photo']) && is_uploaded_file($_FILES['photo']['tmp_name'])
 && $_FILES ['photo'] ['error'] == UPLOAD_ERR_OK) {
    echo $_FILES['photo']['type'], "<br />";
    // Checks if the file is a JPG image
    if ($_FILES ['photo'] ['type'] == 'image/jpeg') {
        $tmp_img = $_FILES ['photo'] ['tmp_name'];
        // Creates an image resource
        $image = imagecreatefromjpeg ( $tmp_img );
        // Tells the browser what type of file
        header ( 'Content-Type: image/jpeg' );
        // Outputs the file to the browser
        imagejpeg ( $image, '', 90 );
        // Frees the memory used for the file
        imagedestroy ( $image );
    }
    else {
        echo "Uploaded file was not a JPG image.";
    }
} else {
    echo "No photo uploaded!";
}
} else {
// If the form was not submitted, displays the form HTML
?>
<form action="test.php" method="post"
enctype="multipart/form-data">
<label for="photo">User Photo:</label>
    <input type="file" name="photo" />
    <input type="submit" value="Upload a Photo" />
</form>
<?php } // End else statement ?>

test.php中有什么?只有test.phpy您将文件发送到test.php,除非您不知道脚本的代码,否则无法找到来自该脚本的错误。很可能php正在打印错误消息,但您无法看到它,因为浏览器试图将其显示为图像。要解决这个问题,首先注释行
标题('Content-Type:image/jpeg')
和后面的两行。然后您可能会看到错误消息,您可以在这里发布。当我对行进行注释时,请对行
标题('Content Type:image/jpeg')进行注释浏览器显示了
警告:imagejpeg():第14行的/opt/lampp/htdocs/simple_blog/test.php中的文件名不能为空
并且我添加了一个空格作为其名称,我得到了
警告:imagejpeg():无法打开流:第14行的/opt/lampp/htdocs/simple_blog/test.php中的权限被拒绝
。。
// Set second param to "null" to directly output the image
imagejpeg ( $image, null, 90 );
imagedestroy ( $image );
// Exit here so that the HTML below is not mixed with the image data:
die();