file download with php (working on apache under localhost) - php

I'm trying to create a file download page. This page when requested should prompt the user to download a file. Here is the source code for the page:
<?php
header('Content-disposition: attachment; filename=zip.zip')
header('Content-type: application/zip');
readfile('zip.zip');
?>
This works ok.
The problems starts when I want to move the file zip.zip from the folder where this script is in. I tried using relative and absolute URLs but I always get strange results,
the browser still prompts for file download but somehow it's just an odd file name converted from the URI I supplied somthing like ".._.._files_zip.zip instead of ../../files/zip.zip.
Any suggestions why this happens?
Thanks

Use basename to get just the file name:
$file = '../../files/zip.zip';
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Type: application/zip');
readfile($file);

Related

Download different file to that requested php

In Joomla auto updater the file is requested from one domain
http://download.abc.com/?ext=addmenu&src=core&pro=1&file=update.zip
which then downloads a different file
/mnt/storage/vhosts/newdomain.com/httpdocs/tmp/addmenu-v1.1.4.zip
I've been trying to mimic this effect by calling a file release.php which is
header("Location: /addmenu/updates/com_addmenu.zip");
but it just downloads release.php and not com_addmenu.zip
I've also tried it with
header('HTTP/1.1 307 Temporary Redirect');
before the header(Location
But I can't get it to work this way. I'm guessing that I'm not able to substitute one file for another but I'm hoping someone can help.
thanks
I understood that you have a zip file on your server which shall be downloaded under a different name by the visitor's browser when s/he opens up your webpage.php.
<?php
$file_url = '/addmenu/updates/release.zip';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Content-disposition: attachment; filename=com_addmenu.zip');
readfile($file_url);

While downloading pdf file from another server forward slash is get replaced with underscore

I am trying to download pdf file from my server. while downloading file it replaces forward slash(/) with underscore and file path become like this(127.0.0.1_file_upload_demoform1.pdf), because of that not able to download file.
here is code.
header('Content-Type: application/pdf');
$download_path = "127.0.0.1/file_upload/demoform1.pdf";
header('Content-Disposition: attachment; filename='.$download_path);
readfile('downloads/'.$download_path);
Can anyone suggest what is issue in it.
I got the answer.
I was trying to download the pdf file from another server. which does not allow me directly download from that location.
Here is my solution.
$content = file_get_contents('http://127.0.0.1/file_upload/demoform1.pdf');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=demoform1.pdf');
echo $content;

php download csv file via web page

I am trying this simple code to download csv file via my web page but didn't work.
<?PHP
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=test.csv');
header('Pragma: no-cache');
echo readfile('test.csv');
?>
the error is "internet explorer can't find "csv_download.php" the file link on my web page
Click here to download the "CSV" file
anything missing?
Your PHP file is not at the URL you expect. Always set the full path where possible:
<a href="/csv_download.php">...

php pdf download getting corrupt

I have a code which force download a pdf file. Code below
$file_name = 'Bv_Ebook.pdf';
$file_url = 'http://' .$_SERVER['HTTP_HOST'].'/sites/default/files/'. $file_name;
header('Content-Type: text/html');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
This code works on our beta server and the pdf is downloaded and can be opened.
On our live server same code downloads the pdf but pdf file cannot be opened, it says:
Unable to open document
File type plain text document (text/plain) is not supported
I have gone though force download pdf and apache headers setting but nothing seems to work.
Any idea what could be the issue?
Check file permission using is_readable() to check if the file is accessible or not. If accessible then check below :
Can you please try to modify your headers to the following:
//We'll be outputting a PDF.
header('Content-type: application/pdf');
//PDF name.
header('Content-Disposition: attachment; filename="'.$file_name.'"');
Then do a file read or what ever, in my case I have used file_get_contents(file_path) + echo to display the content and worked as a charm.

Downloaded filename of file using X-Sendfile

Im using X-Sendfile to send a file instead of readfile. The script processing this, is named download.php and contains this:
$video_file = '/path/to/file/file.mp4';
header('X-Sendfile: '.$video_file);
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; file=\"file.mp4\"');
exit();
But problem is that the downloaded file is always named "download.php" (155Mb), and i would like to download it named as file.mp4. Ive tried several things like:
header('Content-Disposition: attachment; file="file.mp4"');
header('Content-Disposition: attachment; file=file.mp4');
And all other posibilities, but still downloading the file as download.php.
My htaccess file contains: XSendFile On
You are sending an incorrect header. The filename property is called filename, not name:
header('Content-Disposition: attachment; filename="file.mp4"');
See RFC2616 for a detailed description of the header.

Categories