首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP和MySQL向图库添加图像

使用PHP和MySQL向图库添加图像
EN

Stack Overflow用户
提问于 2013-07-10 06:05:38
回答 1查看 6.5K关注 0票数 0

我试图创建一个后端我的照片画廊,这样它将使我更容易上传图片到画廊。

画廊由一个缩略图,一行描述和缩略图连接到一个花哨的盒子。很基本的。(缩略图和花哨框图像使用的图像相同。)

我刚刚开始学习PHP和MySQL的基础知识,并想知道如何使用这段代码:(FYI,这只是库的一部分)。

代码语言:javascript
复制
<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目录中。我只是不知道如何设置数据库和表格来检索图像和文本,并将它们放在图片库中

我会这样设置数据库吗?(我不知道我现在在做什么。)

数据库名称:主库

表:数据- id :id升序

(数据id和数据类型非常重要,因为图库连接到过滤系统。我希望自动生成数据id,并将最新的图像添加到画廊的顶部。)

数据类型:树修整

数据类型: treeremoval

rel: gallery1

rel: gallery2

图片:?

标题:奥扎克湖的树木移除

标题:奥斯奇海滩修剪

然后,对于h5标记

描述:奥扎克湖移除树木

描述:欧扎克湖修剪

对于图像,我使用的是一个max-img边框类,因为站点是响应的,而且我的所有图像大小都为720 px482 is。

css如下所示:

代码语言:javascript
复制
.max-img-border { 
 width:100%;
 height:auto;
 border:solid 2px #FFFFFF;
 margin-bottom:10px;
 }

通过使用这种方法,我已经设置了它,所以我只需要处理缩略图和fancybox图像的一个图像。

我希望我的要求是有意义的。

我知道我要求的可能是很多解释,但任何帮助都将是非常感谢的。即使是一些链接到一个好的教程可能会有帮助。我在谷歌搜索了一两天,但实际上找不到我要找的东西。

此外,如果你看画廊和洗牌的类别,有一个很大的飞跃在过渡。我目前正在修复这个问题。

如果你还需要我的信息,请让我知道,我会更愿意提供它。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-10 06:25:38

您需要一个MySQL表,其中包含有关图像的信息和图像的文件名:

代码语言:javascript
复制
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)
)

你需要制作一个图片上传表格,比如:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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文件上载到服务器。像这样的事情是必要的:

代码语言:javascript
复制
$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");
}

现在,在图库页面上,您需要遍历数据库中的图像。

代码语言:javascript
复制
<?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
}
?>

请注意,上传表格必须受到保护,并且只有合适的人才能使用。您不希望垃圾邮件发送者将随机文件上载到您的服务器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17563512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档