views:

31

answers:

2

Hi,

I am using this PHP script to download a file:

$status=stripos($_SERVER['HTTP_REFERER'],'servername');
if($status===false)
{
    header('Location:http://xyz.com);
}
else
{
    header('Content-disposition: attachment; filename=XXX.pdf');
    header('Content-type: application/pdf');
    readfile('http://www.xyz.com/Downloads/XXX.pdf);
}

However when I download the file in any browser it says it is corrupt.

Please Help!

+1  A: 

But this code works fine

header("Content-type: application/pdf");
header('Content-Disposition: attachment; filename="sample.pdf"');
readfile('/var/www/sample.pdf');

Please change filename=XXX.pdf to filename="XXX.pdf" and check.

Oh sorry!! You should provide a base path or relative pat to the content mean it should be like readfile('/var/www/Downloads/XXX.pdf); rather readfile('http://www.xyz.com/Downloads/XXX.pdf);

Thanks

Muhit
Thanx for the information. However my readfile parameters has the entire file path since it is dynamic and I pass the file path at runtime to the php file and pass it as a query string. header("Content-disposition: attachment; filename=$_GET[disp]"); header("Content-type: $_GET[type]"); readfile("$_GET[file]"); where $_GET[disp] = XXX.pdf $_GET[type] = application/pdf $_GET[file] = 'http:// www.xyz.com/Downloads/XXX.pdf' in this case the file name could change at runtime to a xyz.jpg in that case $_GET[type] would be passed as image/jpeg. Please comment.
Kushal
A: 

I found a solution i would like to share,
In my c# code [on clicking download button]-
Response.Redirect("http://myserver.com/download.php?file=DownloadItem/" + fileName + "&type=" + Common.MimeType(fileName) + "&disp=" + fileName + "");

In my PHP file [http://myserver.com/download.php]
$status=stripos($_SERVER['HTTP_REFERER'],'myserver.com');
if($status===false)
{
header('Location:http://myserver.com/ErrorPage');
}
else
{
header("Content-disposition:attachment;filename= $_GET[disp]");
header("Content-type: $_GET[type]");
readfile("$_GET[file]");
}

Kushal