Php图像不显示
我有这个程序,其中放入他/她的名字,图像的名称,并从文件中选择图像。一切都完美,但形象完美。它不会上传,也不会移动到正确的文件夹。下面是用户上传他/她的形象代码: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">
<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章”文件夹中。我希望它被保存在“图像”文件夹中。该图像是从“图像”文件夹中选择的。这里是页面的样子: Image not showing on php page
如果任何人都可以找到解决方案,这将帮助我很多!谢谢!
在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/";
我没有明白你的意思。它由你完成,上传后的图像应该被重命名为从数据库查询返回的图像的id。 –
忘记我说的,非常感谢,它的工作! –
'$ ImageDir'需要一个结尾斜杠 – Scuzzy
将'$ ImageDir'改为'$ ImageDir =“/ Applications/XAMPP/xamppfiles/htdocs/chapter7/images /”; –