Uli
Uli

Reputation: 2693

PHP - Redirect to download page if file is not available

I'm using PHP to handle file downloads. Currently I'm showing just an information if a file is not available. (Happends if older file version get linked from external websites)

How to redirect directly to the download page instead? (E.g. www.example.com/downloads.html)

if (!is_file($file_path)) {
  die("<center><strong><u>file not available</u><strong></center>"); 
}

Upvotes: 0

Views: 172

Answers (2)

venkat
venkat

Reputation: 2310

Try PHP header function to redirect

https://www.php.net/manual/en/function.header.php

header('Location: www.example.com/downloads.html');

Upvotes: 0

Jon Taylor
Jon Taylor

Reputation: 7905

Using php headers is what you need to redirect. However make sure that you have not echod anything to the page beforehand, headers must be the first thing to appear in your response.

header('Location: http://www.example.com/downloads.html');

You can also use relative urls in here if needed.

And as noted in a comment below you need to http:// protocol otherwise it gets treated as a relative url.

Upvotes: 2

Related Questions