I have a html file that has to be converted into pdf. My code is:
<?php
include("connection.php");
extract($_REQUEST);
$query=mysql_query("select fad_html_name from fad_record where t_id='$word'") or die(mysql_error());
while($result=mysql_fetch_array($query,MYSQL_NUM))
{
extract($result);
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($result as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
}
?>
This isn't working.zip file is being created, but pdf doesn't get downloaded.
Related
I have a one-time link like in this:
<?php
session_start();
$file1 = "img/img1.jpeg";
$key1 = md5($file1 . microtime());
$_SESSION[$key1] = $file1;
$link1 = "download.php?key=" . $key1;
echo "<a href='" . $link1 . "'id='button1'>download</a><br>";
?>
download.php
<?php
session_start();
// Get the key from the query string
$key = $_GET['key'];
// Check if the key exists in the session
if(isset($_SESSION[$key])) {
$file = $_SESSION[$key];
// Remove the key from the session to prevent reuse
unset($_SESSION[$key]);
// Set the content type and headers for the file
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . basename($file));
header("Content-Length: " . filesize($file));
readfile($file);
}
else{
echo("Invalid link");
}
?>
Every time the page is refreshed, it creates a new one-time link that can only be used once.
And if I follow this link, the file will be downloaded. But when I follow it a second time, the link is no longer valid. How can I make the link update when clicking on it (without refreshing the page), so that the link is always valid?
I do not know how to do it? How can this be done? Via ajax? Or how?
Here you go:
<?php
session_start();
$file1 = "img/img1.jpeg";
$key1 = md5($file1 . microtime());
$_SESSION[$key1] = $file1;
$link1 = "download.php?key=" . $key1;
echo "<a href='" . $link1 . "'id='button1'>download</a><br>";
?>
download.php
<?php
session_start();
// Get the key from the query string
$key = $_GET['key'];
// Check if the key exists in the session
if(isset($_SESSION[$key])) {
$file = $_SESSION[$key];
// DO NOT remove the key from the session, to allow reuse
//unset($_SESSION[$key]);
// Set the content type and headers for the file
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . basename($file));
header("Content-Length: " . filesize($file));
readfile($file);
}
else{
echo("Invalid link");
}
?>
It works now. :-)
(This answers your question, but if it's not what you wanted, you might need to be clearer about your objective and reasons.)
I have a local DB table in which many PDF links are saved around 15000. I want to download all that PDF on one click but my problem is its opening PDF not downloading. I was trying this method.
items = Array.from(document.getElementsByTagName("a"));
items.forEach(function(item) {
link = item.href;
if (link.substr(link.length - 4) == ".pdf") {
filename = link.replace(/^.*[\\\/]/, '');
item.download = filename;
item.click();
}
});
You can not download all files using only 1 click. Instead of You can use ZIP Archive Class in PHP.
Make one zip file of all available pdf and download it.
$files = array('pdf1.pdf','pdf2.pdf');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
And Headers Like
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
Thanks for your reply,
this code works for me with zip archive
$files = array('pdflink','pdflink');
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
foreach($files as $file){
$download_file = file_get_contents($file);
$zip->addFromString(basename($file),$download_file);
}
$zip->close();
header('Content-disposition: attachment; filename=file.zip');
header('Content-type: application/zip');
readfile($tmp_file);
?>
How to make live preview a PDF file in my local website ( no internet ) which is the PDF saved in Longblob? I use this script to download the PDF :
<?php
if(isset($_GET['id']))
{
include "../conn.php";
$query = "select * from file where id = '".$_GET['id']."'";
$result = mysql_query($query) or die (mysql_error);
$download = mysql_fetch_array($result);
$name = str_replace('%20', ' ',$download['file_name']);
$type = $download['file_type'];
$size = $download['file_size'];
$content = $download['file'];
header("Content-disposition: attachment; filename=\"".$name."\"");
header("Content-length: ".$size."");
header("Content-type: $type");
echo $content;
exit;
}
?>
I have use almost all open source PDF Viewer plugins like from http://viewerjs.org , but failed to display the wanted PDF file. Need your help guys..
you shoud have a pdf plugin and you should use code looks like :
header('Content-type: application/pdf');
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-Disposition: inline;filename='document.pdf'");
header("Content-length: ".strlen($binary_contents_from_database));
As the title specifies, I get a File is not in specified format error when downloading the file. The following is my code. Also, the downloaded file does not have any cells.
$content = ob_end_flush();
$filename = trim($filename);
$filename = str_replace(array(' ', '/', '\\'), '', $title);
$filename .= "_Export.xls";
header('Content-type: application/vnd.ms-excel; charset=utf-8');
//header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $content;
put:
ob_clean();
ob_start();
before
header('Content-type: application/vnd.ms-excel; charset=utf-8');
and try...
OR
change
header('Content-type: application/vnd.ms-excel; charset=utf-8');
to
header('Content-type: application/msexcel; charset=utf-8');
or
header('Content-type: application/octet-stream; charset=utf-8');
I want data into .xls file...but it doesn't give....
$this->load->view("partial/footer_excel");
$content = ob_end_flush();
$filename = trim($filename);
$filename = str_replace(array(' ', '/', '\\'), '', $title);
$filename .= "_Export.xls";
ob_clean();
ob_start();
header('Content-type: application/vnd.ms-excel; charset=utf-8');
//header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $content;
die();