FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help displaying pdfs using PHP





rookie11
Hi there,

I am currently trying to have a drop down menu which lets me select a PDF from a directory of pdfs and once selected will open and display the PDF.

Any help would be much appreciated, here is the code I have so far.

Code:


<?php

/**
 * Check if we have submit the folder
 * Check if we have submit the file
 */
$post_folder = isset($_POST['folder']) ? $_POST['folder']: null;
$post_file = isset($_POST['file_name']) ? $_POST['file_name'] : null;

/**
 * Built out the Folder submission form
 */
// Set the path of the dir you want displayed...
$path="./track/";
echo '<form method="POST" action="', $_SERVER['PHP_SELF'],'">';
echo '<select name="folder">';
foreach (new DirectoryIterator($path) as $asset){
    if($asset->isDot()) continue;
    if($asset->isDir()){
       /**
        * If we've posted which folder we want to view the contents of
        * then automatically make that selected in the dropdown on page load
        */
        $selected = (isset($post_folder) && $post_folder == $asset->getFileName()) ? 'selected' : '';
        echo "\t<option value='".$asset->getFilename()."' $selected>".$asset->getFilename()."</option>\n";   
    }
}
echo '</select>';

if(isset($post_folder)){
   
    echo '<select name="file_name">';
    $folder_to_iterate = $path.$post_folder;
    foreach (new DirectoryIterator($folder_to_iterate) as $asset){
        if($asset->isFile()){
           /**
            * If we've posted which folder we want to view the contents of
            * then automatically make that selected in the dropdown on page load
            */
            $selected = (isset($post_file) && $post_file == $asset->getFileName()) ? 'selected' : '';
            echo "\t<option value='".$asset->getFilename()."' $selected>".$asset->getFilename()."</option>\n";   
        }
    }
    echo '</select>';
}

$btn_text = isset($post_folder) ? 'Show File Contents' : 'Show Folder Contents';
echo '<input type="submit" value="', $btn_text ,'"/>';
echo '</form>';

if(isset($post_file)){
    $file_to_read = $path.$post_folder.'/'.$post_file;
    echo '<pre>';
    echo htmlentities(file_get_contents($file_to_read));
    echo '</pre>';
}
?>


I think it's not possible to simply echo out the PDF as it returns it as a bunch of nonsense words and numbers.

Someone has told me to read the submitted pdf filename and construct the object-tag, using php to embed this filename into the object-tag.

How exactly would I do that?
Peterssidan
How do you want to display the pdf? I think if you just redirect to the pdf file itself it will open it in the default pdf program or show the open/save file dialog depending on browser and programs installed.
sonam
If you want open .pdf in browser you can just use a tag. In that case user must go back on menu or you can open .pdf in new window.

For displaying on the same page menu and .pdf maybe is the best solution to use iframe (and change content via ajax).

But maybe is there some other way, too.

Sonam
vat322
Hi, That someone who told you about the object tag is right. Here it is
Code:

echo '<object data="'.$pdf_file_name.'" type="application/pdf">Oops! your browser doesn't have a pdf plugin.</object>';


How it works:
This object tag is used embed pdf, audio,videos etc. on webpages. Here, if the browser has ability to read pdf types, it shows the content in data tag, else it executes the error notice written in between the object tags. Hope this helps. Smile[/b]
Related topics
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
Using php to change content of a .txt
Access Java 1.6 Web service using php client
How to code a simple blog using PHP
i want to dezign a website using php
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.