Php 图片未显示

Php image not showing

我有这个程序,其中输入 his/her 名称、图像名称,然后从文件中选择图像。除了图像,一切都完美无缺。它不会上传,也不会移动到正确的文件夹。这是用户上传 his/her 图片的代码:

<!DOCTYPE html>
<html>
<head>
<title>Upload pic to our site</title>
</head>
<body>
<form name="form1" method="post" action="check_image.php" 
enctype="multipart/form-data">
    <table border="0" cellpadding="5">
        <tr>
            <td>Image Title or Caption<br>
                <em>Example: You talkin' to me?</em></td>
            <td><input type="text" name="image_caption" 
id="item_caption" size="55" maxlength="255"></td>
        </tr>
        <tr>
            <td>Your Username</td>
            <td><input type="text" name="image_username" 
id="image_username" size="15" max="255"></td>
        </tr>
        <tr>
            <td>Upload Image:</td>
            <td><input type="file" name="image_filename" 
id="image_filename"></td>
        </tr>
    </table>
    <br>
    <em>Acceptable image formats include: GIF, JPG, JPEG, PNG.</em>
    <p align="center"><input type="submit" name="Submit" 
value="Submit">
    &nbsp;
    <input type="submit" name="Submit2" value="Clear Form">
    </p>
</form>
</body>
</html>

这是显示图像的代码:

<?php
//connect to database
$link = mysqli_connect("localhost", "root", "", "moviesite");
if (!$link) {
    "Connection lost: " . mysqli_connect_error();
}

//make variables available
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");

//upload image and check for image type
$ImageDir = "/Applications/XAMPP/xamppfiles/htdocs/chapter7/images";
$ImageName = $ImageDir . $image_tempname;

if (move_uploaded_file($_FILES['image_filename']['tmp_name'], 
$ImageName)) {
    //get info about the image being uploaded
    list($width, $height, $type, $attr) = getimagesize($ImageName);

    switch ($type) {
        case '1':
            $ext = ".gif";
            break;
        case '2':
            $ext = ".jpg";
            break;
        case '3':
            $ext = ".png";
            break;

        default:
            echo "Sorry, but the file you uploaded was not a GIF, JPG, 
or  PNG.";
            echo "Please hit your browser's 'back' button and try 
again.";
            break;
    }

    //insert info into images
    $insert = "INSERT INTO image
              (`image_caption`, `image_username`, `image_date`)
              VALUES
              ('$image_caption', '$image_username', '$today')";
    $insertresults = mysqli_query($link, $insert);

    $lastpicid = mysqli_insert_id($link);

    $newfilename = $ImageDir . $lastpicid . $ext;

    rename($ImageName, $newfilename);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Here is your pic</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1><br><br>
<p>Here is your picture you just uploaded to our servers:</p>
<img src="<?php echo $ImageDir . $lastpicid . $ext; ?>" align="left">
<strong><?php echo $image_username; ?></strong>
This image is a <?php echo $ext; ?>image.<br>
It is <?php echo $width; ?> pixels wide
and <?php echo $height; ?> pixels high.<br>
It was uploaded <?php echo $today; ?>
</body>
</html>

图像正在保存到 "chapter7" 文件夹中。我希望它保存在 "images" 文件夹中。图片选自 "image" 文件夹。这是页面的样子: Image not showing on php page

如果有人能找到解决方案,那将对我有很大帮助!谢谢!

在 HTML img 标签中,src 属性值只能是通过网络浏览器可用的文件。

在您的代码中设置了 $ImageDir = /Applications/XAMPP/xamppfiles/htdocs/chapter7/images,因此浏览器无法从该地址访问目录及其文件,因为浏览器无法访问 /Application/XAMPP...

如果你想显示这个图片你需要在img标签的src属性中回显这个路径: echo "chapter7/images/" . $lastpicid . $ext;

也改变

$ImageDir to $ImageDir = "/Applications/XAMPP/xamppfiles/htdocs/chapter7/images/";