网站照片库

发布于 2024-11-17 00:07:50 字数 122 浏览 0 评论 0原文

使用照片库时,是否有一种“标准”方式在网络上存储照片? 将照片存储在 mysql 数据库中是不好的做法吗?我应该将照片存储在文件夹中,并将链接存储在数据库中吗?

当谈论存储照片以在网站上使用时,什么是“良好实践”?

Is there a "standard" way of storing photos on the web, when working with photo galleries?
Is it bad practice to store photos in a mysql database? Should I store the photo in a folder, and store the link in a database?

What is "good practice" when talking about storing photos, to use on a website?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-11-24 00:07:50

您可以使用不同的 BLOB 数据类型将照片存储在数据库中。如果照片数量过多或过大,数据库的性能可能会受到影响,并且大小会急剧增加。本质上,如果您有 100 张 10Mb 的照片,那么数据库将增长 1,000Mb 或大约 1GB 的大小。备份将花费很长时间才能执行,如果包含图像的表由于某种原因而损坏,这将导致比您愿意处理的更令人头痛的事情。

正如 imoda 建议的那样,我的建议是将它们存储在硬盘上,并在数据库中链接到它们,您将在数据库中保存所有元数据和文件位置的链接。最佳做法是将照片的文件名存储在数据库中,并重命名该文件以使用主键的值作为文件名。例如,第 5 行与照片“x”关联,其中 x 是文件名,将其存储在硬盘上驱动器并将照片重命名为文件名 5,然后当您访问它进行下载等操作时,您将从数据库中的第 5 行提取实际文件名,并在下载之前重命名它。如果您碰巧上传两个或多个具有相同文件名的文件,这将防止意外覆盖该文件。

You can store photos in your database using the different BLOB data types. If the photos are excessive in quantity, or extra large, the database my start to take a performance hit and will increase in size drastically. Essentially, if you have 100 10Mb photos, that's 1,000Mb or around a Gb of size that the database will grow. Backups will take forever to execute, and if a table containing the images corrupts for whatever reason, it'll induce more headache than you'll care to deal with.

My suggestion as imoda recommends is to store them on a hard drive, and link to them in the database where you'll hold all of the metadata and a link to the location of the file. Best practice is to store the photo's file name in the database, and rename the file to use the primary key's value as the file name As an example, row 5 associates to photo "x" where x is the filename, store it on the hard drive and rename the photo to the file name of 5, and then when you go to access it for things like download, you'll pull the actual file name from row 5 in the database and rename it before you download it. That'll keep from accidentally overwriting the file if you happen to upload two or more files with the same file name.

滥情哥ㄟ 2024-11-24 00:07:50

99% 的情况下您不想将照片存储在 MySQL 数据库中。如果处理不当,将会非常耗费资源。将照片存储在网站的目录中,并将它们的路径存储在数据库中。

99% of the time you don't want to store a photo in a MySQL database. It's incredibly resource intensive if done improperly. Store your photos in directories on your website and store the path to them in your database.

夏雨凉 2024-11-24 00:07:50

我建议使用 Flickr 添加/删除并重新排列您的图片。使用 Flickr 的优点是您不必编写任何复杂的代码或托管图片。您可以使用 PHP 和 Flickr API 通过您的个人资料 ID 检索图片。
1. 使用 Flickr 创建应用程序
2.下载phpFlickr并将其放在服务器的根目录下
https://code.google.com/p/phpflickr/downloads/list
将此 PHP 代码添加到您的 gallery.php 文件

这是带有分页功能的 PHP Gallery 的代码

<div class="portfolio-grid">



    <?php

        $key = "92d657c8dc79a29108d846a5fd121a29";  //your API key
        $nsid = "108400461@N02";            //your NSID

        // get page number from the url - if there isn't one - we're on page 1
        $page = isset($_GET['page']) ? $_GET['page'] : 1;

        // inclue the core file
        require_once('phpFlickr/phpFlickr.php');
        // Fire up the main phpFlickr class 
        $f = new phpFlickr($key);
        $f->enableCache("fs", "cache");

        $photos = $f->people_getPublicPhotos($nsid, NULL, NULL, 20, $page);
        $pages = $photos[photos][pages]; // returns total number of pages
        $total = $photos[photos][total]; // returns how many photos there are in total
    ?>


    <!--END script Flick photos -->
    <div>
        <?php
            echo '<div id="container">';
            // loop through each photo
             foreach ($photos['photos']['photo'] as $photo) {

            // print out a link to the photo page, attaching the id of the photo
                 echo '<div class="item" style="float:left;">
                        <a class="swipebox-isotope" href="' . $f->buildPhotoURL($photo, 'Large') . '" title="A.S.A.C - The African Student's Association of Concordia"><img class="gallery-img" src="' . $f->buildPhotoURL($photo, 'medium') . '" alt="' . $photo['title'] . '" title="' . $photo['title'] . '"/></a>
                      </div>';

            // end loop
            }
            echo '</div>';
        ?>

    </div>

      </div>
  <h2 id="nav">
    <?php
        // Some simple paging code to add Prev/Next to scroll through the thumbnails
        $back = $page - 1;
        $next = $page + 1;
        // if it's not the first page
        if($page > 1) {
        echo "<a href='?page=$back'>« <strong>Prev</strong></a>";
        }




        // if not last page
        if($page != $pages) {
        echo "<a style='float:right' href='?page=$next'><strong>Next</strong> »</a>";}
        // a quick bit of info about where we are in the gallery
        echo"<h3 style='text-align:center;'>Page $page of $pages <br/>$total photos</h3>";

    ?>
</h2>

I suggest to use Flickr to add/Remove et rearrange your picture. The advantage of using Flickr is that you don't have to write any complex code or host the pictures. You can use PHP and the Flickr API to retrieve the pictures using your profile ID.
1. Create an App with Flickr
2. Download phpFlickr and put it on the root of your server
https://code.google.com/p/phpflickr/downloads/list
add this PHP code to your gallery.php file

Here is the code for the PHP Gallery with Pagination

<div class="portfolio-grid">



    <?php

        $key = "92d657c8dc79a29108d846a5fd121a29";  //your API key
        $nsid = "108400461@N02";            //your NSID

        // get page number from the url - if there isn't one - we're on page 1
        $page = isset($_GET['page']) ? $_GET['page'] : 1;

        // inclue the core file
        require_once('phpFlickr/phpFlickr.php');
        // Fire up the main phpFlickr class 
        $f = new phpFlickr($key);
        $f->enableCache("fs", "cache");

        $photos = $f->people_getPublicPhotos($nsid, NULL, NULL, 20, $page);
        $pages = $photos[photos][pages]; // returns total number of pages
        $total = $photos[photos][total]; // returns how many photos there are in total
    ?>


    <!--END script Flick photos -->
    <div>
        <?php
            echo '<div id="container">';
            // loop through each photo
             foreach ($photos['photos']['photo'] as $photo) {

            // print out a link to the photo page, attaching the id of the photo
                 echo '<div class="item" style="float:left;">
                        <a class="swipebox-isotope" href="' . $f->buildPhotoURL($photo, 'Large') . '" title="A.S.A.C - The African Student's Association of Concordia"><img class="gallery-img" src="' . $f->buildPhotoURL($photo, 'medium') . '" alt="' . $photo['title'] . '" title="' . $photo['title'] . '"/></a>
                      </div>';

            // end loop
            }
            echo '</div>';
        ?>

    </div>

      </div>
  <h2 id="nav">
    <?php
        // Some simple paging code to add Prev/Next to scroll through the thumbnails
        $back = $page - 1;
        $next = $page + 1;
        // if it's not the first page
        if($page > 1) {
        echo "<a href='?page=$back'>« <strong>Prev</strong></a>";
        }




        // if not last page
        if($page != $pages) {
        echo "<a style='float:right' href='?page=$next'><strong>Next</strong> »</a>";}
        // a quick bit of info about where we are in the gallery
        echo"<h3 style='text-align:center;'>Page $page of $pages <br/>$total photos</h3>";

    ?>
</h2>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文