linux - Can PHP (with Apache or Nginx) check HTTP header before POST request finished? -


here simple file upload form html.

<form enctype="multipart/form-data" action="upload.php" method="post">     send file: <input name="userfile" type="file" />     <input type="submit" value="send file" /> </form> 

and php file pretty simple too.

<?php die(); 

as see, php script nothing in server side. when uploading big file, process still cost long time.

i know, php code executed after post process ended. php must prepare array named $_post , $_files before first line code parsed.

so question is: can php (with apache or nginx) check http header before post request finished?

for example, php extensions or apache modules.

i told python or node.js can resolve problem, want know if php can or not.

thanks.

================ update 1 ================

my target try block unexpected file-upload request. example, generated unique token post target url (like http://some.com/upload.php?token=268dfd235ca2d64abd4cee42d61bde48&t=1366552126). in server side, code like:

<?php define(my_salt, 'mysalt'); if (!isset($_get['t']) || !isset($_get['token']) || abs(time()-$_get['t'])>3600 || md5(my_salt.$_get['t'])!=$_get['token']) {//token check     die('token incorrect or timeout'); } //process file uploaded /* ... */ 

code looks make sense :-p cannot save bandwidth expected. reason php code runs late, cannot check token before file uploading finished. if upload file without correct token in url, network , cpu of server still wasted.

any suggestion welcome. lot.

the answer yes because open source. first, background: (i'm going talk nginx, apache same.)

the upload request isn't sent php backend right away -- nginx buffers upload body php app isn't tying 100mb of ram waiting guy upload via 300 baud modem. downside app doesn't find out upload until it's done or done uploading (depending on client_body_buffer_size).

but can write module hook different "phases" internally nginx. 1 of hooks called when headers done. can write modules in lua, it's sill complex. there may module send "pre-upload" hook out script via http. that's not great performance.

it's won't need module. nginx.conf files can need. (i.e. route request different scripts based on headers, or return different error codes based on headers.) see page examples of header checking (especially "wordpress w/ w3 total cache using disk (enhanced)"): http://kbeezie.com/nginx-configuration-examples/

read docs, because common header-checking needs have directives of own (i.e. client_max_body_size reject request if content-length header big.)


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -