php download csv file via web page - php

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">...

Related

Download existing file from server to client using php

I'm trying to make a distant client able to download an existing file(csv) named "source.csv" in my local machine using php.
After downloading the file the client should have a file named sourceCopy.csvwith the same content as the source file.
Here's what I've tried :
$file_url = 'source.csv';
header('Content-Type: application/csv');
header("Content-Transfer-Encoding: Binary");
header('Content-disposition: attachment; filename="sourceCopy.csv"');
readfile($file_url);
The file is downloaded but it doesn't match the source file !

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;

To download a csv file in specific folder automatically

I want to download a csv file using a link automatically in a specified folder by running a cron job using PHP.
I tried the below code ,but it gets downloaded in download folder by default.
can any one show me the right way to do it.The code used is below.
$link="http://labcase.com/wrt/search.php?format=csv&sortby=reqnum|DESC&Search=non_closed_req&state[]=New&state[]=Pending%3A+Delivery&state[]=Pending%3A+Installation&state[]=On+Hold&state[]=Pending%3A+More+Info&state[]=Assigned&state[]=Working&state[]=Pending%3A+Approval&orgs[]=125&orgs[]=25&bldg[]=BGL04%2CBGL11%2CBGL12%2CBGL13%2CBGL14%2CBGL15%2CBGL16%2CBGL17%2CBGL20%2CBGL22%2CBGL25%2CBGL26%2CBGL43&business_unit=all";
header('Content-Disposition: attachment; filename=example.csv');
header("Content-Type: application/force-download");
header('Pragma: no-cache');
readfile($link);
You can use this:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
Take a look here

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.

file download with php (working on apache under localhost)

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);

Categories