开发者

best way to store images and then display them

I have a form that searches a mysql db for whatever the user is after, and returns the results on the same page in a div (using ajax then php to search mysql)...

The results are ads, as cars for example, and I want each ad to have a unique image associated with it so that it displays the images next to the ads...

Storing im开发者_JAVA百科ages in BLOBS arent really a good solution I have heard...

How would I do this the best way?

Thanx


A fast way will be to store images in a folder giving unique filenames in folder or separate folders if you want to put images of different categories in separate places. After searching the available ads, read associated unique file-names and server to client.

Suppose you save images related to car ads in folder /images/cars/. User searches for Audi S6 and the result returns 5 ads.

Now we will proceed based on the naming of the image files. If you give file related to each ad unique name and put that name into record for that ad then simply get that name and create image URL as follows:

/images/cars/ + result_row('image_name')

If you are naming images based on id of ad record then use this scheme:

/images/cars/ + result_row('id')

If you mean that at first you were sending image bytes in response, then you don't need to do that. simply send the path constructed and use it in src property of img tag.

PS:- My PHP skill are not very good now!


Typically, you would want to store image records in the database as details about the image and then a file path to the actual image. Then, store the images in the regular file system.

As a side best-practice, you typically will want to store the path relative to some common root, then you can append the file root so the store of images can be moved around. ie> store ads/cars/1005.jpg and then append a root of 'C:/myApp/images/'


Each unique ad will have a unique identifier in the database. You could create image subdirectories for a given set of images and save the images based on their unique identifier.

i.e. advertisement 506 could be stored in /img/506.jpg

Ultimately you would query the database for the advertisement and then you would assume you are loading the image with filename identifier + '.jpg'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜