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);
?>
Related
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.
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "https://vibrantgujarat.com/pressclippingsnew.htm"; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
I Have a page which has more than 500 press clippings information along with date, name, media name and image path.
I want to download all of them using script but I Don't know how to write download script.
Here is link
Any help would be great.
Thank You.
Checkout the following function, which is not working fully, you need to try out some changes to it.
function saveImageAs(){
var images = document.getElementsByTagName("img");
for(var i=0;i<images.length; i++){
var imgOrURL= images[i].src;
window.win = open(imgOrURL);
setTimeout('win.document.execCommand("SaveAs")', 0);
}
}
<?php
set_time_limit(0);
//File to save the contents to
$fp = fopen ('download.zip', 'w+');
$url = "https://vibrantgujarat.com/pressclippingsnew.htm";
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
//give curl the file pointer so that it can write to it
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);//get curl response
//done
curl_close($ch);
This will help you to download images and contents for the 1st page.
Hope this helps.
This question already has answers here:
How to force file download with PHP
(12 answers)
Closed 6 years ago.
I have files uploaded on server folder and its path is saved in database. sample view of table is:
id name resume
1 N1 resume/abc.doc
2 N2 resume/def.pdf
On a click of a button i wish to download the files (format of file would mostly be docx, pdf) from the server to the system. The code that i have written downloads a file but it is either empty or corrupted. Can anyone please tell how to download files from server
Download
download.php
$candidateid=$_GET['candidateid'];
$filename = "Filename.docx";
header("Content-Disposition: attachment; filename=\"$filename\"");
$flag = false;
$sql = "SELECT resume FROM candidates where id='".$candidateid."' ";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
if (!$flag)
{
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
echo implode("\t", array_values($row)) . "\r\n";
}
}
Using the candidateid you can fetch the file name from your table and then store in a variable and then write the code
$file = '/path/to/your/dir/'.$file;
if(!$file){ // file does not exist
die('file not found');
} else {
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// read the file from disk
readfile($file);
}
I have a following function that downloads the file whose name is passed as a parameter in the url. Here is the code:
public function actionDownloadFile($filename)
{
$file = Yii::app()->request->getBaseUrl(true) . '/upload/digitaluploads/' . $filename;
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
}
This function is called from view file when the user clicks on download button from the view:
<a class="digital-download" href="<?php echo Yii::app()->request->getBaseUrl('true'); ?>/site/downloadfile/filename/<?php echo urlencode($digital_download['filename']); ?>">Click here to download</a>
This works for filename that don't have spaces like it will work for file with the name like somefile.mp3 but it does not work for the files that have spaces in that. i.e it fails for the files whose name contains spaces like 'maid with the flaxen hair.mp3'.
As you can see I have even encoded the parameter in the url using urlencode. I tried using decode in action as well but all I get is the broken page.
Anyone?
use this function to avoid space and other possible problems:
function fullescape($in)
{
$out = '';
for ($i=0;$i<strlen($in);$i++)
{
$hex = dechex(ord($in[$i]));
if ($hex=='')
$out = $out.urlencode($in[$i]);
else
$out = $out .'%'.((strlen($hex)==1) ? ('0'.strtoupper($hex)):(strtoupper($hex)));
}
$out = str_replace('+','%20',$out);
$out = str_replace('_','%5F',$out);
$out = str_replace('.','%2E',$out);
$out = str_replace('-','%2D',$out);
return $out;
}
found here: http://php.net/manual/en/function.urlencode.php
try this :
str_replace(' ', '%20', 'your url here');
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));