我正在尝试为我的照片库创建一个后端,这样我就可以更轻松地将图像上传到图库。
图库由缩略图图像,一行描述和缩略图连接到一个精美的盒子组成。很基本的。 (缩略图和精美的盒子图像使用相同的图像。)
我刚刚开始学习PHP和MySQL的基础知识,并想知道如何使用这段代码:(仅供参考,这只是图库的一部分。)
<li data-id="id-12" data-type="treeremoval">
<div class="column grid_3">
<a class="fancybox" rel="treeremoval" href="images/gallery/1.jpg" title="Tree Removal Lake of the Ozarks">
<img src="images/gallery/1.jpg" alt="Tree Removal Lake of the Ozarks" class="max-img-border"></a>
<h5>Lake of the Ozarks Tree Removal </h5>
</div>
</li>
<li data-id="id-11" data-type="treetrimming">
<div class="column grid_3">
<a class="fancybox" rel="treetrimming" href="images/gallery/2.jpg" title="Osage Beach Tree Trimming">
<img src="images/gallery/2.jpg" alt="Osage Beach Tree Trimming" class="max-img-border"></a>
<h5>Lake of the Ozarks Tree Trimming</h5>
</div>
</li>
并创建一个管理系统,我可以将图像上传到图库,而无需在每次客户端向我发送图像时手动进入并调整代码。
很多,我想创建一个表单,我可以设置参数并将图像上传到图库页面。
使用上面的代码,如何在列表中添加更多图片?
我想我要问的是如何设置数据库和表格,并在html中添加适当的php语法以获得我正在寻找的结果?
我了解如何创建表单以将图像上传到ftp目录。我只是迷失在设置数据库和表格以检索图像和文本并将它们放在图库中
我会像这样设置数据库吗? (仅供参考我不知道我现在在做什么。)
数据库名称:maingallery
表: data-id:id ascending
(数据标识和数据类型非常重要,因为图库连接到过滤系统。我希望自动生成数据标识,并将最新图像添加到图库的顶部。)< / p>
data-type:treetrimming
data-type:treeremoval
rel:gallery1
rel:gallery2
图片:...... ???
标题:奥沙克的树木去除湖
标题:欧塞奇海滩树修剪
然后是h5标签
标题:欧扎克斯树去除湖 标题:欧扎克斯树修剪湖对于我正在使用max-img-border类的图像,因为该网站是响应式的,我的所有图像大小为720px x 482px
css看起来像这样:
.max-img-border {
width:100%;
height:auto;
border:solid 2px #FFFFFF;
margin-bottom:10px;
}
通过使用这种方法我设置了所以我只需要处理缩略图和fancybox图像的一个图像。
我希望我所要求的是有道理的。
我知道我要求的内容可能需要解释,但任何帮助都会非常感激。 甚至一些指向良好教程的链接也可能有所帮助我搜索谷歌一两天,但无法找到我正在寻找的确切内容。
此外,如果你看一下画廊并对各个类别进行洗牌,那么过渡期就会有很大的变化。我正在努力解决这个问题。
如果您需要我的信息,请告诉我,我将非常愿意提供。
谢谢!
答案 0 :(得分:1)
您需要一个MySQL表,其中包含有关图像的信息以及图像的文件名:
CREATE TABLE images (
id int(3) not null auto_increment,
data_type varchar(128) not null,
title varchar(256) not null,
file_name varchar(64) not null,
primary key(id)
)
您必须制作图片上传表单,例如:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Data type: <input type="text" name="dataType"><br>
Title: <input type="text" name="title"><br>
Image to upload: <input type="file" name="image"><br>
<input type="submit" value="Upload">
</form>
用于处理上传文件和添加数据库条目的PHP脚本:
<强> uploader.php 强>
<?php
$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);
$fileName = basename($_FILES["image"]["name"]);
$target_path = "images/gallery/".$fileName);
if (file_exists($target_path))
{
echo "An image with that file name already exists.";
}
elseif (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
// The file is in the images/gallery folder. Insert record into database by
// executing the following query:
// INSERT INTO images
// (data_type, title, file_name) VALUES('$dataType','$title','$fileName')
echo "The image was successfully uploaded and added to the gallery :)";
}
else
{
echo "There was an error uploading the file, please try again!";
}
?>
请注意,此脚本不安全,它允许人们将例如.php文件上传到服务器。这样的事情是必要的:
$allowed_extensions = array("jpg","jpeg","png","gif");
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
if (!in_array($extension,$allowed_extensions))
{
die("Only these file types are allowed: jpg, png, gif");
}
现在在图库页面上,您想要遍历数据库中的图像。
<?php
$images = mysql_query("SELECT * FROM images");
while ($image=mysql_fetch_assoc($images))
{
?>
<li data-id="id-<?=$image["id"] ?>" data-type="<?=$image["data_type"] ?>">
<div class="column grid_3">
<a class="fancybox" rel="<?=$image["data_type"] ?>" href="images/gallery/<?=$image["file_name"] ?>" title="<?=$image["title"] ?>">
<img src="images/gallery/<?=$image["file_name"] ?>" alt="<?=$image["title"] ?>" class="max-img-border"></a>
<h5><?=$image["title"] ?></h5>
</div>
</li>
<?php
}
?>
请注意,上传表单必须受到保护,并且只适用于合适的人员。您不希望垃圾邮件发送者将随机文件上传到您的服务器上。