Well, The main problem for any popular [ wide ] Images Hosting website .. is the bandwith !

and this problem can be solved with Multi-Server programming .. with PHP

I have some ideas in my brain .. it's like :
- administrator provides me with 2 - 3 servers info
- everytime i go upload an image i choose one randomly ( or depends on something ) to upload the image to


the problem now, will i create Over-Servers connection ? and what about the DB ? will it be on main server ? or everything will be on the main server .. just the image location will be on another server ?

Well, The main problem for any popular [ wide ] Images Hosting website .. is the bandwith !

and this problem can be solved with Multi-Server programming .. with PHP

I have some ideas in my brain .. it's like :
- administrator provides me with 2 - 3 servers info
- everytime i go upload an image i choose one randomly ( or depends on something ) to upload the image to


the problem now, will i create Over-Servers connection ? and what about the DB ? will it be on main server ? or everything will be on the main server .. just the image location will be on another server ?

What you can do is have a the name of the image determine the server to save the image on. That way you don't have to hit a central db just to know which server its on. It will also take care of randomizing it.

eg: if an image filename starts with a letter between a and g save it on server 1. From g - m server 2. etc.

I've never done distributed servers but I'd think the simplest would be to have a main server w/ db of image entries. the other servers just host the images. The main server generates the HTML or other markup that will reference the images on the other servers.

I agree with that .. if the images URLs are direct links .. but what if :

download.php?file=20

I need something more than just " hosting images on other server "

- download.php should be on the main server ( which has DB + HTML and main script ) and that case .. we wouldn't do much except increasing the bandwith ( cuz i would reach the actual file from another server )


- download.php would be on other servers and i include the full path when uploading an image .. like " imgs23.any.com " but in that case .. when he open " http://imgs23.any.com/download.php?id=20 " it will lead to 2 points :
+ download.php must be created or copied on everyserver .. and I can something noobie here ..

+ download.php should have the access to DB ( main server ) to inquery ( 1 query ) to fetch file info and then show it from the same server ( imgs23 ) ..

So , does it worth it ? or there is something i can have it done with servers-administration ? like virtual servers ? sub servers ? something like that .. programming-independant ..

I agree with that .. if the images URLs are direct links .. but what if :

download.php?file=20

I need something more than just " hosting images on other server "

- download.php should be on the main server ( which has DB + HTML and main script ) and that case .. we wouldn't do much except increasing the bandwith ( cuz i would reach the actual file from another server )


- download.php would be on other servers and i include the full path when uploading an image .. like " imgs23.any.com " but in that case .. when he open " http://imgs23.any.com/download.php?id=20 " it will lead to 2 points :
+ download.php must be created or copied on everyserver .. and I can something noobie here ..

+ download.php should have the access to DB ( main server ) to inquery ( 1 query ) to fetch file info and then show it from the same server ( imgs23 ) ..

So , does it worth it ? or there is something i can have it done with servers-administration ? like virtual servers ? sub servers ? something like that .. programming-independant ..

The question would be why do you need a url such as http://imgs23.any.com/download.php?id=20 ?

How different is that from having this on the main server:

<?php 

// retrieve the image file by id from the database
$imagename = $db->getImageById(20);

// print this to HTML. The image server does not need to run php or access the db
echo '<img src="http://imgs23.any.com/'.$imgname;

?>

This would become even more efficient if you were to display a large number of images since the main server that serves the HTML will only access the db once to get the list of images by their IDs. The image server will require no further processing and just give out images blindly.

eg:

<?php 

// retrieve the image file by id from the database
$limit = 20;
$category = 'animals';
$images = $db->getImageByCategory($category, $limit);

foreach ($images as $image) {
  echo '<img src="http://imgs23.any.com/'.$imgname;
}

?>

now if the main server had the DB on the same machine, it would be very efficient. If you did it the other way:

<?php 

// retrieve the image file by id from the database
$limit = 20;
$category = 'animals';
$image_ids = $db->getImageIDsByCategory($category, $limit);

foreach ($images as $img_id) {
  echo '<img src="http://imgs23.any.com/download.php?id='.$img_id;
}

?>

Then on the image server you'd need a php script and database call to a remote database. You'd also have to pipe the images through PHP, which means for large images, the apache will have to keep the PHP process running for the duration of the download. If the database is remote, then this means another network connection adding latency and using bandwidth.

The only reason I can see that you would need download.php?id=n is if you needed to control the download with PHP, such as to prevent leaching, or have members only donwload, etc.

Exactly ..

I have user groups .. which contains 2 main groups :
- Visitors
- Members

and for example, you can add Website-Text on image for visitor, but removes it for a member !
or speed limitation, download/hour .. etc

You can't just simple do such a thing to the real image .. and the real image should be kept as it's, and then apply effects On Fly depending on the user group ..


I aprecaite your code, and I understand it .. but as you said

this means another network connection adding latency and using bandwidth.

So, it's not efficient ..

But you inspired me with an idea .. I can use XML to get file info ..
for example,
http://imgs23.any.com/download.php?id=20
first I alert ( http://www.any.com/get_info.php?id=20 ) and get results in XML format or serilizaed or whatever .. So that the actual DB connection will be established on Main Server .. but still i fetch file info through a connection between imgs23 and main server .. or any server and main server ..

So,
- As administrator adds a new Server, he supplies server info including FTP info
- Script copies a download.php copy to server directory ( Just Once at setup )
- download.php will alert get_info.php and supply it with the id, and get all info about that id
- show image or whatever


drawbacks :
- Security, anyone tracing server responses and requests through any header-analyzer .. would get the connection and parameters, and get info for any file he wants ( I don't know if this is a feature or a drawback :S )

- Still there is more load cuzed of connection but i guess it's simple one .. or it's one of that kinds that u can stand with ..

what u think ?
Apreciated ..

This article has been dead for over six months. Start a new discussion instead.