0

I have a PHP file that performs a query to an external API using a query parameter to retrieve the image from the database. This particular API doesn't return the image, but rather generates a new URL that can be used for a short period of time (associated cookie involved).

So my PHP file might be found at:

myserver.com/getFile.php?id=B9590963-145B-4E6A-8230-C80749D689WE

which performs the API call which generates a URL like this:

myserver.com/Streaming_SSL/MainDB/38799B1C4E38F1F9BCC99D5A4A2E0A514EEA26558C287C4C941FA8BA4FB7885B.png?RCType=SecuredRCFileProcessor&Redirect

which I store in a PHP variable - $fileURL. I then use:

header('Location: '. $fileURL);

to redirect the browser to the URL that shows the image. This is all working well, but has created an issue with a service that I integrate with. This service caches the 2nd (redirected URL) which causes problems, as it essentially only works the first time it is generated due to the use of the session cookie. I need to come up with a solution that will allow them to cache a URL that will continue to work that shows the image.

I'm wondering is there a way that I can download the image and show that somehow, without having to redirect the browser to a new location here and thus allowing the original URL to continue working if it is cached?

user982124
  • 4,416
  • 16
  • 65
  • 140
  • _"is there a way that I can download the image"_ [`file_get_contents()`](https://www.php.net/manual/function.file-get-contents.php) – Phil Jun 10 '20 at 02:13
  • Does this answer your question? [Download File to server from URL](https://stackoverflow.com/questions/3938534/download-file-to-server-from-url) – Don't Panic Jun 10 '20 at 02:27

1 Answers1

1

To get image or file do this:

$getImg = file_get_contents('myserver.com/getFile.php?id=B9590963-145B-4E6A-8230-C80749D689WE')

Name the file

$fileName = 'name.png'

Save the image on your server (in directory) to view it later or show somewhere else with url.

$saveFile = file_put_contents($fileName , $getImg);

Load the file like you wanted after you have saved it.

header('Location: '. $fileName);

Hope it helps.

Always Helping
  • 14,316
  • 4
  • 13
  • 29
  • `file_put_contents()` returns an integer. You don't want to redirect that result but instead use something like `readfile`() – Phil Jun 10 '20 at 02:36
  • Thanks - this is working so far in my tests. Is it possible to avoid the redirect step `header('Location: '. $fileName);` and have to just output the image to the browser without having to redirect to a new URL? I'm not wanting to keep these images around on my server so looking for ways to have these deleted after x number of days. – user982124 Jun 14 '20 at 10:55
  • Glad that its all working for you. To open in the same browser you have to either use js window.open('url', '_self') OR you can in the PHP you can echo html 'a' tag with href like this: echo 'Open Image'; – Always Helping Jun 14 '20 at 22:08
  • 1
    To answer you second question regarding deleting file after x number of days. You need to do a cron job which is run after x number of day at certain date and time which will check if the file for example was created like 7 days ago. Ideally you should save file details in database to and run query with cron job which will then delete files and it data after x number of days. – Always Helping Jun 14 '20 at 22:10