I have a simple system where a user adds a url of a file he wants to download (for example an image that is on the freepik website), and then via API my PHP system generates the file to be downloaded for the user. The problem is that the API does not return a download link, but an endpoint where I should make a request passing some parameters including my API token to download. So, so that the user doesn’t see this url with my API token, I used this simple code to get the API file and make it available for the user to download:
<?php
$filename = $_GET['filename'];
$url = $_GET['url'];
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename="$filename"");
readfile(urldecode($url));
This works for small files but when the file is too big it gives out memory limit error.
So my idea would be to rescue pieces of that file that is in the API using javascript and PHP, so that the user could download it little by little, but I don’t know if that would be possible.
Is there a strategy I could use here?