不显示 php 图像



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

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

图像正在保存到"第 7 章"文件夹中。我希望它保存在"图像"文件夹中。图像是从"图像"文件夹中选择的。页面的外观如下: 图像未显示在php页面上

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

在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/";

最新更新