我可以帮助你创建一个PHP图片展示网站,这个网站将允许用户上传图片并显示在网页上,我们将使用HTML、CSS和PHP来实现这一功能。

目录结构
我们需要一个基本的目录结构:
/image_gallery
/uploads
index.php
upload.php
styles.css `index.php`
这是主页面,用于展示已上传的图片。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Image Gallery</h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" required>
<button type="submit">Upload Image</button>
</form>
<h2>Uploaded Images</h2>
<div class="gallery">
<?php
$images = scandir('uploads');
foreach ($images as $image) {
if ($image !== '.' && $image !== '..') {
echo '<img src="uploads/' . $image . '" alt="' . $image . '">';
}
}
?>
</div>
</body>
</html> `upload.php`
这是处理图片上传的脚本。

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["image"]["name"]);
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check !== false) {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)) {
echo "The file " . htmlspecialchars(basename($_FILES["image"]["name"])) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
} else {
echo "File is not an image.";
}
} else {
echo "No file uploaded.";
}
header("Location: index.php");
exit();
?> `styles.css`
这是样式文件,用于美化页面。
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
h1, h2 {
text-align: center;
}
form {
display: flex;
justify-content: center;
margin: 20px 0;
}
form input[type="file"] {
margin-right: 10px;
}
.gallery {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.gallery img {
margin: 10px;
max-width: 200px;
max-height: 200px;
object-fit: cover;
} `uploads` 文件夹
确保你有一个名为uploads 的文件夹,并且它有写权限,如果没有,请创建它并设置适当的权限。
代码展示了一个简单的PHP图片展示网站,用户可以上传图片,这些图片将被保存到uploads 文件夹中,并在主页上显示出来,你可以根据需要进一步扩展和优化这个基本框架。

以上就是关于“php图片展示网站_图片展示”的问题,朋友们可以点击主页了解更多内容,希望可以够帮助大家!
本文来源于互联网,如若侵权,请联系管理员删除,本文链接:https://www.9969.net/85027.html