-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.

Saurin Dashadia
  • 1,140
  • 11
  • 25
  • Does this answer your question? [How to force file download with PHP](https://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php) – Simone Rossaini Jul 10 '20 at 13:38
  • That is to output the file. I need opposite what it does. I have given the URL: https://filehippo.com/download_mozilla-firefox-64/post_download/ I need to download from it using PHP. – Saurin Dashadia Jul 10 '20 at 13:41

1 Answers1

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.
Onimusha
  • 3,348
  • 2
  • 26
  • 32
  • 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. – Saurin Dashadia Jul 10 '20 at 14:15
  • For cookies, you can try it with `curl` instead https://stackoverflow.com/questions/1797510/file-get-contents-receive-cookies – Onimusha Jul 10 '20 at 14:17
  • 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. – Onimusha Jul 10 '20 at 14:22
  • 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 :) – Saurin Dashadia Jul 10 '20 at 14:33
  • 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. – Onimusha Jul 10 '20 at 14:37