Jump to content

Server Load question (accessing images, a little MySQL/PHP/htaccess)


Recommended Posts

Ok, I have a folder on my webserver with 300,000 images in it (growing daily) which are hotlinked to from all over the internet. This is what I want to happen, that isn't my problem, I have a server that can handle it and it is my business model.

 

The problem is that things got really big faster than I could manage, and now I have one folder with 300,000 images in it. And periodically they all disappear. My host swears black and blue that nobody is deleting them, and it's a symptom of their system not being able to handle that many images. Whatever, I've had many arguments with them about it, that's not what this is about either.

 

Basically I want to restructure the folders, but I don't know which way will be best in the long term. This basically comes down to which way is the smallest strain on the server.

 

The images have numerical filenames (1.gif, 2.gif etc).

 

I will be splitting them into subfolders. This is where I don't know what to do.

 

Should I redirect all requests to a php file which would lookup in the database which folder that image is in and serve it from that folder?

 

OR

 

Should I split them into numerical folders. (1-100 in a folder, 101-200 in another), and redirect intelligently to the correct folder? If so, can htaccess do this or would I need the PHP middleman again?

 

I hope that was clear enough, I just need some guidance as to how to serve up multiple images without causing undue strain on the server. Thanks.

Link to comment
Share on other sites

You should be able to do this using mod_rewrite and bit of PHP. PHP will then serve the correct image from the necessary folder group based on the requested url. Most operating systems/filesystems have limits for the maximum number of files within in directories.

 

I'd say it would be best to split them into numerical folders, something like 0-1000, 1001-2000 etc.

 

Could you post some example urls of how your images are currently being requested.

Link to comment
Share on other sites

I assume you are going to split the images into numerical folders like so:

0-1000

1001-2000

2001-3000

.. etc ...

10001-20000

20001-30000

... etc ...

100001-110000

110001-120000

... etc ...

290001-300000

 

and that all your images are gifs.

 

if so the following code should work:

<?php

if(isset($_GET['img']) && is_numeric($_GET['img']))
{
    $imgN = $_GET['img'];

    $imgL = strlen($imgN);

    if($imgN >= 0 && $imgN <= 1000)
    {
        $folder = '0-1000';
    }
    else
    {
        $t = substr($imgN, 0, -2);

        $min = (floor($t/10)*1000)+1;
        $max =  ceil($t/10)*1000;

        $folder = $min.'-'.$max;
    }

    $imgP = './user/'.$folder.'/'.$imgN.'.gif';

    if(file_exists($imgP))
    {
        header('Content-type: image/gif');
        echo file_get_contents($imgP);
    }
    else
    {
        echo $imgP . ' does not exist';
    }
}

?>

 

RewriteEngine On

RewriteRule ^([0-9]+).gif$ img.php?img=$1

 

img.php works out the correct folder the image should be in.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.