Jump to content

PHP Server-Side Execution


Doqrs

Recommended Posts

Hello

Here is the scenario:

I have a PHP page that a visitor visits after uploading a file.

we'll call it "uploaded.php"

within uploaded.php is some code which gets the $_FILES data, displays all the appropriate stuff, etc.

In uploaded.php i would like to call another php file, example: updateID3.php?id=123, where id is a col in a mysql database. I want the updateID3.php file with params to be executed independent of the Client viewing the uploaded.php page.

So the browser continues to parse and display the uploaded.php page while updateID3.php with parameters is running on the server (almost like a cron job but running instantly and only once)

I hope i was descriptive enough.

Thanks
Link to comment
Share on other sites

Easiest way is to write a function, save it in your 'other' file and then 'include' that file into your uploaded.php script and call the function. That way when the uploaded.php script executes it will also execute the function you wrote in the included file.
Link to comment
Share on other sites

The only problem with that is it will be executing the function while the page is rendering. I want something similar to a cron job, which is independent of client actions. In the example i used above, when the user goes to uploaded.php, i want the server to run updateID3.php?id=123 regardless of whether the user closes out of uploaded.php or not, IE it is happening server-side.
The functions on updateID3.php?id=123 are not quick, and would take a long time to render the page before uploaded.php is displayed if the file is simply included.
Link to comment
Share on other sites

Ok, so you don't want it to run each time someone accesses the uploaded.php script. Even if you're going to use 'cron' then you'll need to have something that executes each time someone accesses the uploaded.php file that writes the data you need into a text file or database, for example, that would contain the necessary details you need for your update parameters like user_id, etc. Then, set up a cron to run something like update_cron.php that runs the queries you need by accessing that text file (or database tables).
Link to comment
Share on other sites

I do want it to run every time someone is on uploaded.php (it only occurs after file uploads) because i want the server to transcode the file format but this takes a lot of time.

During heavy traffic times, i think it would be a lot better to have multiple instances of updateID3.php running rather than have a cron job run every minute and pick up and transcode all of the uploads in the last minute (at 3am this could be none, where as it could be a dozen around 9pm).




Link to comment
Share on other sites

Hmmm...ok, here's the problem. A script can't execute unless it's told to execute. That's exactly what cron does...tells things to execute at specific times. Otherwise i'm not aware of any magical solution that allows scripts to operate on their own. As far as I can see you have the two choices. Run a cron as often as you wish or have a sub-script run simultaneously with your main script that updates the data you're seeking.
Link to comment
Share on other sites

What you can do is use the register_shutdown_function function: http://us3.php.net/register_shutdown_function

[code]
        //stuff you do at the begining of page

function stuff_uploadID3PHP_does(){
global $id_this_function_needs;

        //code here
}

register_shutdown_function('stuff_uploadID3PHP_does');

        //Rest of Page
[/code]

This will run that block of code when the page is done.
You may also need to use ignore_user_abort (http://us3.php.net/ignore_user_abort), but I doubt it...
Link to comment
Share on other sites

Option #3! Thanks Deltran. :)

How it would work would be dependent upon how you handle the data. For example, if your uploaded.php script writes info to a MySQL database then this secondary function would need to access it if you want to transfer some of that data to another location.
Link to comment
Share on other sites

I've realized that the function i am attempting to do is similar to that of youtube converting your video to .flv format after you upload it. You can continue to browse the site with no functional adherents, but the server is still processing an assigned request on your behalf. .. maybe this helps?
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.