通过ajax发送响应,但继续PHP脚本

时间:2022-11-07 01:25:29

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.

我有一个脚本通过http将文件上传到我的网络服务器,我需要的是服务器向http帖子发送响应,然后继续运行一些命令。

For example

file is posted via http

文件通过http发布

Server saves the file the inserts data into the database and sends a response to the http.

服务器将文件插入数据库保存到数据库中并向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

请帮忙

1 个解决方案

#1


0  

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.

为确保获得最佳用户体验,您应该通过推迟任何耗时的任务来确保脚本快速执行。例如,您可以创建任务队列并使用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.

或者,如果您决定使用问题中描述的解决方案,那么在成功上载文件后使用AJAX可能会更好。当用户导航离开网页时,AJAX请求可能会终止,因此您仍需要确保服务器继续执行您的脚本。

BTW, You should also run flush() if you actually want to send something to the client.

顺便说一下,如果你真的想要发送一些东西给客户端,你也应该运行flush()。

#1


0  

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.

为确保获得最佳用户体验,您应该通过推迟任何耗时的任务来确保脚本快速执行。例如,您可以创建任务队列并使用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.

或者,如果您决定使用问题中描述的解决方案,那么在成功上载文件后使用AJAX可能会更好。当用户导航离开网页时,AJAX请求可能会终止,因此您仍需要确保服务器继续执行您的脚本。

BTW, You should also run flush() if you actually want to send something to the client.

顺便说一下,如果你真的想要发送一些东西给客户端,你也应该运行flush()。