Is it possible to display via PHP 2 pdf files in the browser? I want that the second file gets attached to the first file. With one file i did it with:
header("Content-Type: application/pdf");
header('Content-Disposition: inline; filename="filea.pdf"');
readfile(*path*);
I tried to attache the 2. file only with readfile but it didnĀ“t worked. Is there a simple way to display the 2 files?
Try PDFtk Server. After you install it, you can execute it like this from your PHP script:
exec('pdftk filea.pdf fileb.pdf cat output filec.pdf');
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="filec.pdf"');
readfile('filec.pdf');
Related
I have a php page which downloads zip files. Normally after the download, it automatically revert back the user to a previous page(myfiles.php) using header('location:myfiles.php');.
When I execute the page, it bring me to myfiles.php but the download pop up won't show up, thus preventing me to download my zip file. When I remove the line header('location:myfiles.php');, I am able to download my zip file as expected.
Below is an extract of my code.
//Some codes
if(file_exists($zip_name)){
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
session_start();
$_SESSION['correct']="Files downloaded sucessfully";
header('location:myfiles.php');
Can you please help me finding a way to fix it? Thank you.
Think about what those headers are doing
These 2 and the readfile
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
are sending a file to the current page that is on the browser.
This one, which will run as well as the code above
header('location:myfiles.php');
attempts to tell the browser to go to another page. If it did do that then the file you sent to the browser would just disappear
So basically done together as one flow, they do not make sense!
Also you cannot send a header('location:...) after any actual data has been sent to the browser, which of course you did when you ran the readfile($zip_name);
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
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">...
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);
For example, if I set some headers to download a file from my server I would do
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
And then just output the data of the file
Is it possible to set a remote url in the Content-Disposition?
Example header('Content-Disposition: attachment; filename="http://remote.location/downloaded.pdf");
Or am I just thinking about it in the wrong way?
No, you would just do a 302 redirect.
No.
As Matthew suggested is to use redirect.
Other option is download file from script, then just output the downloaded file.