tags:

views:

35

answers:

1

I'm not awfully knowledgeable about HTTP, but my understanding is that an HTTP request consists of a bunch of headers and then the body.

Is it possible for PHP to receive the request headers, make a decision based on what's been received, and then either abort the request or continue to accept the body?

This would be useful when receiving an uploaded file which is very large. I could therefore avoid the user wasting time in uploading a huge file only to discover the server won't accept it (file exists already). I know I could send a separate XHR request to do this, but I want to know if it's possible without that.

So, in short, can PHP cancel an HTTP Request after receiving only the headers?

+3  A: 

The details of the HTTP connection are usually handled by the web server. So, it isn't possible to close the connection prematurely from PHP.

It would be possible to implement some kind of "filter" on the web server to scan for the headers you want to avoid, but this would likely involve creating a custom module, depending on your web server.

srkiNZ84
Thanks, it's what I suspected.
Rafael
Huh, my edit didn't show up. I also asked whether you think the only decent solution is to launch a separate XHR request to see if the file exists already. I'm not thrilled about doing this, since 99% of the time it'll be a waste of time, but I'd like to know what the "done" thing is here.
Rafael
What do you mean by "file exists already"? Are you referring to the file name or the file itself? If you mean the file name, then you're right in that an XHR request would be the best way to handle this. If you mean the file itself, then you need to get the whole file to compare. You have to be careful as two files can have the same name but be completely different, or have different names but be the same file. Never trust user input.
srkiNZ84
Yeah you're right. I'm just going to have to upload the whole file and check if the file itself is the same. I guess they could have same size as well, so would I have to compare md5 hashes of them?
Rafael
Yes, that's right, to be sure that they are or aren't same file you would have to compare md5 hash of each file.
srkiNZ84
Thanks for all your help.
Rafael