Php图像没有显示

时间:2017-07-30 04:29:34

标签: php image gd

我有这个程序,其中放入他/她的名字,图像的名称,并从文件中选择图像。一切都很完美,但形象。它没有上传,也没有移动到正确的文件夹。以下是用户上传他/她的图片的代码:

<!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>

图像被保存到&#34;第7章&#34;夹。我想让它保存在&#34;图像&#34;夹。图像选自&#34;图像&#34;夹。以下是页面的外观: Image not showing on php page

如果有人能找到解决方案,那将有助于我!谢谢!

1 个答案:

答案 0 :(得分:0)

在HTML img标签中,src属性值只能是通过Web浏览器提供的文件。

在您的代码中设置$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/";