简体   繁体   中英

Get file from remote server using httpful php library

`I have a file stored on a strato hidrive which I'm able to access directly using webdav and authentication like so:

https://username:password@webdav.hidrive.strato.com/path/to/my/file.zip

When I paste that in my address bar I can directly download the file. But I'm writing a small file getter script which accepts the filename as a parameter in the URL and it will generate the download:

http://example.com/getfile.php?filename=file.zip

That should pop up a download dialog the same way the direct link would

I want to use the httpful php library to download the file if possible (not cURL)

Ended up being quite simple:

$response = Request::getQuick($url);

$path = parse_url($url, PHP_URL_PATH);
$filename = substr($path, strrpos($path, '/') + 1);
header('Content-Type: ' . $response->content_type);
header('Content-Disposition: attachment; filename="' . $filename . '";');
header('Content-Length: ' . $response->headers['content-length']);
readfile($url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM