How to download file Using PHP that has delayed force download?
Asked Answered
M

1

-1

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.

Mutiny answered 10/7, 2020 at 13:30 Comment(2)
Does this answer your question? How to force file download with PHPCuster
That is to output the file. I need opposite what it does. I have given the URL: filehippo.com/download_mozilla-firefox-64/post_download I need to download from it using PHP.Mutiny
C
0
  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.
Connotative answered 10/7, 2020 at 14:9 Comment(5)
Yes, those download works on cookies check I believe. I have tried already using the URL from data-qa-download-url but it did not work on new tab.Mutiny
For cookies, you can try it with curl instead #1798010Connotative
Also check downloadIframe.src = '{THE_URL_HERE}' in the originating page source code. I believe that will work using the method in the answer above.Connotative
This is only one example URL, there should be a common solution for such different URLs. I cant add a custom logic for different URLs :)Mutiny
Not every file hosting web site will use the same method. The solution would obviously have to be modified for each use case depending on how the file is delivered. The answer you're looking for as far as I can tell does not exist.Connotative

© 2022 - 2024 — McMap. All rights reserved.