we are creating a website for hotel booking. we need to store a large number of images. we think it would be a better option to store images in the filesystem and store the path in the database. But do we have to manually save them? We are using web services from another website to get the images. is there a way to save the images dynamically in the filesystem??
It depends on the database, and how you are serving up the images. In general it is better to save the images to disk, depending on how you are delivering them to the client.
Getting the images is usually a matter of some process on the server downloading them from websites and saving them. On many systems you could use wget or curl to download the images and save them.
It also depends on how you are getting the data. If it is some inline binary via XML or something, then you will need to extract that using the features of your application language, and save it to disk.
The mechanics of how to do that vary wildly depending on the implementation language and the hosting operating system.
You can use PHP's file get contents function or CURL to download all the images you want to the disk or simply refering the foreign image to your clients and you won't need to store them locally on the server.
If you like Python check the Mecanize lib and BeautifulSoup to parse XML if you need.
Storing in disk vs storing in database has it's beneficts. If you need to scale, it's easier and you can always have a lighttpd or a nginx http servers dedicated to images or simply put it out on other server to balance bandwidth.
I think that you should store both remote (web service) and local (filesystem) location in the database, with initial file system location blank. If a user requests an image for the first time, download it, update the file field and show it. With this concept you will only have images your clients need.
You can use S3 to save the images to. There are existing S3 libraries out there that allow you to manipulate buckets to save/delete images.
The steps would be as follows:
- using
fopen
,file_get_contents
,curl
(whatever your preference) get the file contents at the URL that the web service specified. - using a PHP S3 class, put the file contents up on S3, giving the file a unique name
- store the image name in the DB
Note: Don't save the full path. if you were to ever need to change image servers for some reason, you don't want to have to filter out a server name. Rather use a constant in your code: define( "IMG_SERVER", "http://imagserver.s3.amazonaws.com/" );
and store 2009/07/hampton_inn_90210.jpg
in the DB.
You can thumbnail images using the PHP GD library (overlay a branded PNG). Google "php thumbnail library," there are good ones out there.