简体   繁体   中英

How to download file Using PHP that has delayed force download?

I am at a situation, where I need to download files from the URL, it is easy with the direct file URLs like https://somedomain.com/some-path/somefile.exe

file_put_contents( $save_file_loc, file_get_contents($url_to_download);

But what to do when you have delayed force download from the URL which actually prints HTML and how to differentiate those URL?

Example URL: https://filehippo.com/download_mozilla-firefox-64/post_download/

EDIT: On above url the file download starts using JS, as I tested with blocking JS and download did not start.

Thanks in advance for your help.

  1. Read the html of the URL using file_get_contents
  2. Find the URL of the file within the HTML. You'll have to visit the page and view source to locate the URL. In your example of https://filehippo.com/download_mozilla-firefox-64/post_download/ it's found in between data-qa-download-url="https://dl5.filehippo.com/367/fb9/ef3863463463b174ae36c8bf09a90145/Firefox_Installer.exe?Expires=1594425587&Signature=18ab87cedcf3464363469231db54575665668c4f6&url=https://filehippo.com/download_mozilla-firefox-64/&Filename=Firefox_Installer.exe"
  3. As you may have noticed, the page may have pre-approved the request so it's not guaranteed to work if the host has checks using cookies or other methods.
  4. Create a regex based on the above to extract the URL using preg_match
  5. Then file_get_contents the URL of the file to download it.

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