I have a script that uploads a file to my webserver via http, what i need is the server to send a response to the http post but then continue to run some commands.
For example
file is posted via http
Server saves the file the inserts data into the database and sends a response to the http.
But i need the server to continue to run after the response and continue running some editing on the file.
example php
function uploadFile(){
$imageName = $tmpname . '.jpg';
move_uploaded_file ($_FILES["foto"]["tmp_name"], $targetDir . $imageName);
$data = Array('ALL THE DATABASE DATE')
$returnid = $this->uploader_model->addData($data);
//I NEED IT TO RETURN THIS TO THE AJAX HTTP REQUEST
echo json_encode(array(
'returned' => 'Successfully uploaded..',
'id' => $returnid
));
//I NOW NEED IT TO KEEP RUNNNG SOME EDITING ON THE IMAGE HERE IE..
shell_exec(" run some image editing here 2>&1");
}
I have seen ignore user abort like
ignore_user_abort(1); // run script in background
set_time_limit(0); // run script forever
but does this mean the script will just run forever and crash the server if it is getting multiple requests? just a bit confused on how to use this function correctly in this scenario
Any help please
Your script won't run forever unless you make it, e.g. by using an infinite loop. Having said that, that's not necessarily a good way to go about it.
To ensure the best user experience, you should make sure your script executes quickly by deferring any time-consuming tasks. For example, you could create a task queue and run the queued tasks with cron.
Alternatively, if you do decide to use the solution you described in your question, you may be better off running that with AJAX after a file has been successfully uploaded. AJAX requests may be terminated when the user navigates off the webpage, so you'd still need to make sure the server continues executing your script.
BTW, You should also run flush() if you actually want to send something to the client.