I was thinking of doing a head request with cURL, was wondering if this is the way to go?
相关问题
- Views base64 encoded blob in HTML with PHP
- Angular RxJS mergeMap types
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
Using a HEAD request and checking for
Content-Length
is the standard way to do it, but you can't rely on it in general, since the server might not support it. TheContent-Length
header is optional, and further the server might not even implement the HEAD method. If you know which server you're probing, then you can test if it works, but as a general solution it isn't bullet proof.The best solution which follows the KISS principle
If you don't need a bulletproof solution you can just do:
Yes. Since the file is remote, you're completely dependent on the value of the
Content-Length
header (unless you want to download the whole file). You'll want tocurl_setopt($ch, CURLOPT_NOBODY, true)
andcurl_setopt($ch, CURLOPT_HEADER, true)
.I'm guessing using curl to send a HEAD request is a nice possibility ; something like this would probably do :
And will get you :
This way, you are using a HEAD request, and not downloading the whole file -- still, you depend on the remote server send a correct Content-length header.
Another option you might think about would be to use
filesize
... But this will fail : the documentation states (quoting) :And, unfortunately, with HTTP and HTTPS wrappers,
stat()
is not supported...If you try, you'll get an error, like this :
Too bad :-(