brosjr Posted February 1, 2012 Share Posted February 1, 2012 Hi all, On my project some PDF documents and images are storage outside the web public folder, so I just can't reach then just with a simple link and I need to display then on a page. $fileDir = '../data/contas'; For images I use the code below (this solution I found on web): $fileDir = '../data/contas/'; function data_uri($conta, $mime) { $contents = file_get_contents($conta); $base64 = base64_encode($contents); return "data:$mime;base64,$base64"; ) <img src="<?php echo data_uri($conta, 'image/png');?>" width="720"> When I see the HTML code I got <img src="data:image/png;base64,/9j/4AAQSkZJRgABA...(huge amount of string)...FXYq/wD/2Q==" width="720"> and the images are displayed correctly. The problem occurs on PDF files, I use the same function, as follow below and the PDF document is not displaying, there is no error message just a gray background where the document were to be shown. <EMBED SRC='<?php echo data_uri($conta, 'application/pdf');?>#view=FitH,top&toolbar=1&navpanes=0&scrollbar=1&zoom=scale' WIDTH="720" HEIGHT="900" ALIGN="TOP"></EMBED> When I look at the HTML code it shows: <EMBED SRC='data:application/pdf;base64,JVBERi0xLjIK...(huge amount of string)...YK#view=FitH,top&toolbar=1&navpanes=0&scrollbar=1&zoom=scale' WIDTH="720" HEIGHT="900" ALIGN="TOP"></EMBED> Any ideia how can I show the PDF files? Or there is another way to access the files outside web public folder on server to display then? Thankx Danilo Jr. Quote Link to comment https://forums.phpfreaks.com/topic/256200-pdf-out-of-web-root-and-encoding-error-view/ Share on other sites More sharing options...
kicken Posted February 1, 2012 Share Posted February 1, 2012 I've never really tried a data uri in an embed, it could be that the browser does not support it there (or maybe the plugin that handles the embed). A fairly typical method of access files outside the web root is to just create a proxy script. Eg: example.com/serveFile.php?file=blah <?php $file = $_GET['file']; //Make sure you sanitize $file to ensure it points to a valid file //do any other checks such as ensure the user is logged in and has permission if necessary //if all checks are pass then header('Content-type: application/pdf'); //or vary based on file type which you'll have to get somehow header('Content-length: '.filesize($file)); header('Content-disposition: attachment; filename="'.basename($file).'"'); readfile($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/256200-pdf-out-of-web-root-and-encoding-error-view/#findComment-1313421 Share on other sites More sharing options...
brosjr Posted February 6, 2012 Author Share Posted February 6, 2012 Thank you kicken but that worked just for downloading the pdf as file it didn't help to opening the document on the page. But I thied the below object tag and it worked, not as I expected once I can't handle with it's zoom options, maybe because of encoded strings, but it's fine. <object data='<?php echo data_uri($conta, 'application/pdf');?>' type='application/pdf' width='720' height='900'> </object> Thankx Danilo Jr. Quote Link to comment https://forums.phpfreaks.com/topic/256200-pdf-out-of-web-root-and-encoding-error-view/#findComment-1315138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.