skip to Main Content

I need to check the availability of a file on the server and then doing some reaction related to the file. I have the file name and path in each client and server side, then I have two ways to check the availability. One from the client side and another one from the server side.

First way:

Client side:

function check_file()
{
    $.ajax({
        url:'http://www.example.com/somefile.txt',
        type:'HEAD',
        error: function()
        {
            //file not exists
            //check again after 5 seconds
            setTimeout(function(){
                check_file();
            }, 5000);
        },
        success: function()
        {
            //file exists
            //start doing other actions related to the file
            action_1();
        }
    });
}

function action_1()
{
    $.ajax({
        url:'http://www.example.com/action_1.php',
        success: function(data)
        {
            var result = $.parseJSON(data);
            if (result.success)
            {
                //Changes were applied successfully.
                //doing some DOM stuff and notifying the client

                //check again after 5 seconds
                setTimeout(function(){
                    check_file();
                }, 5000);
            }
        }
    });
}

Server side (action_1.php):

//update database
echo '{success: true}';

Second way:

Client side:

function action_2()
{
    $.ajax({
        url:'http://www.example.com/action_2.php',
        success: function(data)
        {
            var result = $.parseJSON(data);
            if (result.success)
            {
                //Changes were applied successfully
                //doing some DOM stuff and notifying the client
            }

            setTimeout(function(){
                action_2();
            }, 5000);
        }
    });
}

Server side (action_2.php):

if (file_exists('somefile.txt'))
{
    //update database
    echo '{success: true}';
}
else
{
    echo '{success: false}';
}

In the first way, I am calling the server twice (Request/Response/Request, please note first request is only requesting for the HEAD), and it is using Apache's default scenario to check the file availability (that I don’t know how it is working).

But in the second way, I am calling the server once, and it is using the PHP file_exists() that it mean it need to load PHP environment first, then execute action_2.php.

Because the file is not mostly existent and because it need to handle maybe millions of requests, Then can you please guide me which way is better and why?

2

Answers


  1. While the first way is probably faster, presuming that in most cases the file does not exist, I’d go with the second way.

    The first way is faster than the second way as it does not call the PHP handler but just the default static file handler. I didn’t tested that, but I’d assume that checking if a file exists and returning an error (404) of some kind should be noticeable faster than calling the PHP interpreter, which then executes the PHP file.

    However, the second way is IMO much more cleaner and better understandable than the first way, offering a better concentration of the “file checking” logic which is spread over server & client in the first way.

    Login or Signup to reply.
  2. First way is faster for sure, especially if there is Nginx on server instead of Apache, but you must ensure that your fetching “somefile.txt” is not cached in any way.

    Both, your browser and web server likes to cache static files.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search