<?php /* Template Name: PDF */ ?> <?php // place this code inside a php file and call it f.e. "download.php" $uploads = wp_upload_dir(); $ppp = explode('/', $uploads[path]); $path = "/".$ppp[1]."/".$ppp[2]."/".$ppp[3]."/".$ppp[4]."/".$ppp[5]."/".$ppp[6]."/09/"; //$path = $uploads['path']."/"; // change the path to fit your websites document structure $fullPath = $path.$_GET['download_file'].".pdf"; 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"); // add here more headers for diff. extensions header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download break; default; header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); } 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; // example: place this kind of link into the document where the file download is offered: // <a href="download.php?download_file=some_file.pdf">Download here</a> ?> <a href="pdf?download_fileq=<?php echo $filed_name[0]; ?>" ><img src="<?php bloginfo('template_directory') ?>/images/pdf.jpg" /><span class="pdf">Download the <?php echo $filed_name[0]; ?></span></a>
How to download PDF file using WordPress codex
24 Saturday Aug 2019
Posted WordPress
in