简体   繁体   English

图像存储-更好的方法

[英]Image storage - better way

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). 用户填写有关产品的详细信息,然后从计算机中选择一些图片(5张或更多图片)。 When submitted i will store product details in a table called Products . 提交后,我会将产品详细信息存储在名为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. 对于上传的图像,我创建一个像这样的文件夹: http://mysite.com/uploads/123/ : http://mysite.com/uploads/123/这里123是新添加的产品的产品ID。 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. 也就是说,当添加新产品时,我的脚本将创建一个精美的uri文本。 Eg: http://mysite.com/this-is-my-new-product . 例如: http://mysite.com/this-is-my-new-product : 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 ? 为此,我必须保留一个单独的表,其中包含产品ID和文件名? 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. 还是扫描文件夹(例如: http://mysite.com/uploads/123/*.jpg : http://mysite.com/uploads/123/*.jpg )并逐个回显图像(即),是否好? without using a separate table ? 不使用单独的表?

  • is it ok if use the fancy uri for the filenames of images? 将花式uri用作图像的文件名可以吗? 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. ? 例如: /uploads/123/this-is-my-new-product1.jpg/uploads/123/this-is-my-new-product2.jpg/uploads/123/this-is-my-new-product3.jpg等? So, users will see path of first image as http://mysite.com/uploads/123/this-is-my-new-product1.jpg . 因此,用户将看到第一个图像的路径为http://mysite.com/uploads/123/this-is-my-new-product1.jpg This is physical address. 这是物理地址。 Will it help in SEO ? 对SEO有帮助吗? Or it is bad practice ? 还是不好的做法? If this is good, then to list all images i should use scanddir() ? 如果这很好,那么要列出所有图像,我应该使用scanddir()

Please guide me through correct path. 请引导我通过正确的路径。 Thanku 感谢你

1.i suggest you to create another table to store image filenames with product ids. 1.i建议您创建另一个表来存储带有产品ID的图像文件名。

2.for image filenames, use some unique random name along with the orginal file name, you can use PHP's uniqid to generate filenames, or you can use timestamps to name the file. 2.对于图像文件名,请使用一些唯一的随机名称以及原始文件名,您可以使用PHP的uniqid生成文件名,也可以使用时间戳来命名文件。 Lets say if the image name is myimage.jpg, it would be better to save it as 23208349984_myimage.jpg 假设图片名称为myimage.jpg,最好将其另存为23208349984_myimage.jpg

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. 一种折衷方法是重命名上载的图像,1.jpg,2.jpg等,然后简单地在数据库中登录上载图像的数量 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. 最后一种选择是将图像作为Blob存储在数据库中,但是我不是DB专家,所以我不知道这样做有多广泛或明智。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM