dougu4027 2012-08-06 10:31
浏览 21
已采纳

图像存储 - 更好的方式

in my project i store images(uploaded by user) in a folder, for each respective product.

For example to add a product a form is displayed to user. User fills details about product and selects some images from his computer(5 or more pictures). When submitted i will store product details in a table called Products. For images uploaded, i create a folder like this: http://mysite.com/uploads/123/ here 123 is product id of product that is newly added. And all images are saved inside it.

Questions:

  • how to keep track of images uploaded(or filenames of it) for later displaying? that is, when a new product is added, my script will create a fancy uri text. Eg: http://mysite.com/this-is-my-new-product. So if user visits this link, it should show all images belonging to that product. For this, I have to keep a separate table with product ids and the filenames ? And query this table and echo filenames from it? Or is it good to scan the folder(example: http://mysite.com/uploads/123/*.jpg) and echo images one by one, ie. without using a separate table ?

  • is it ok if use the fancy uri for the filenames of images? Example: /uploads/123/this-is-my-new-product1.jpg, /uploads/123/this-is-my-new-product2.jpg, /uploads/123/this-is-my-new-product3.jpg, etc. ? So, users will see path of first image as http://mysite.com/uploads/123/this-is-my-new-product1.jpg. This is physical address. Will it help in SEO ? Or it is bad practice ? If this is good, then to list all images i should use scanddir() ?

Please guide me through correct path. Thanku

  • 写回答

2条回答 默认 最新

  • dongyi7669 2012-08-06 10:36
    关注

    As you say, you either need some link between the data and the images, or be prepared to scan the folder and just output what you find. The latter is more computationally expensive and not altogether graceful as an approach.

    A compromise might be to rename the images, as they're uploaded, 1.jpg, 2.jpg etc and then simply log in the DB the number of images uploaded. Example:

    if ($res = $db->query("query to get product row here")) {
        $row = $res->fetch_assoc();
        if (isset($row['num_imgs'])) {
            for($i=0; $i<$row['num_imgs']; $i++) {
                $img_path = $product_folder_uri."/".($i+1).".jpg";
                if (file_exists($img_path))
                    echo "<img src='".$img_path."' />";
            }
        }
    }
    

    If you were unwilling to rename the images from their uploaded names, you would need to log their names in the DB, presumably in their own table and via a foreign key to the product table.

    A final option would be to store the images as blobs in the database, but I'm no DB expert so I don't know how widespread or advisable this is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题