Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PDF to Open from Drop Down Menu

    Not sure if this is the forum area for this, but I'll give it a try. I have a drop-down list that generates the section option depending on what user chooses. I would like that option chosen, to open the PDF from the submit button. Can't figure out how to do this. Is this even possible without having a database created? It's only going to be a couple of pdfs. Thanks for any help!

    I have 2 folders on my server. Each house the pdfs.

    Here is the php code:
    PHP Code:
    <html>
    <head>
    <title>PHP Test</title>
    </head>
    <body>
    <?php  
     
    $parent_directory 
    '.'
    $file_types 'pdf'
     
    //===================================================// 
    // FUNCTION: directoryToArray                        // 
    //                                                   // 
    // Parameters:                                       // 
    //  - $root: The directory to process                // 
    //  - $to_return: f=files, d=directories, b=both     // 
    //  - $file_types: the extensions of file types to   // 
    //                 to return if files selected       // 
    //===================================================// 
    function directoryToArray($root$to_return='b'$file_types=false) { 
      
    $array_items = array(); 
      if (
    $file_types) { $file_types=explode(',',$file_types); } 
      if (
    $handle opendir($root)) { 
        while (
    false !== ($file readdir($handle))) { 
          if (
    $file != "." && $file != "..") { 
     
            
    $add_item false
            
    $type = (is_dir($root"/" $file))?'d':'f'
            
    $name preg_replace("/\/\//si""/"$file); 
     
            if (
    $type=='d' && ($to_return=='b' || $to_return=='d') ) { 
              
    $add_item true
            } 
     
            if (
    $type=='f' && ($to_return=='b' || $to_return=='f') ) { 
              
    $ext end(explode('.',$name)); 
              if ( !
    $file_types || in_array($ext$file_types) ) { 
                
    $add_item true
              } 
            } 
     
            if (
    $add_item) { 
              
    $array_items[] = array ( 'name'=>$name'type'=>$type'root'=>$root); 
            } 
          } 
        } 
    // End While 
        
    closedir($handle); 
      } 
    // End If 
      
    return $array_items

     
     
     
    if (isset(
    $_POST[pickfile])) { 
     
      
    // User has selected a file take whatever action you want based 
      // upon the values for folder and file 
     
    } else { 
     
        echo 

    <html> 
    <head> 
      <script type="text/javascript"> 
        function changeFolder(folder) { 
          document.pickFile.submit(); 
        } 
      </script> 
    </head> 
     
    <body>'

     
     
    echo 
    "<form name=\"pickFile\" method=\"POST\">\n"
     
    $directoryList directoryToArray($parent_directory,'d'); 
     
    echo 
    "<select name=\"folder\" onchange=\"changeFolder(this.value);\">\n"
    foreach (
    $directoryList as $folder) { 
      
    $selected = ($_POST[folder]==$folder[name])? 'selected' ''
      echo 
    "<option value=\"$folder[name]\" $selected>$folder[name]</option>\n";  

    echo 
    '</select><br><br>'
     
    $working_folder = ($_POST[folder]) ? $_POST[folder] : $directoryList[0][name]; 
     
    $fileList directoryToArray($parent_directory.'/'.$working_folder,'f',$file_types); 
     
    echo 
    "<select name=\"file\">\n"
    foreach (
    $fileList as $file) { 
      echo 
    "<option value=\"$file[name]\">$file[name]</option>\n";  

    echo 
    '</select><br><br>'
     
    echo 
    "<button type=\"submit\" name=\"pickfile\">Submit</button>\n"
     
    echo 
    "</form>\n"
    echo 
    "</body>\n"
    echo 
    "</html>\n"
     

    ?>


    </body>
    </html>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You need to send a header() directive to the browser that says "hey browser, here's a PDF. Do you know what to do with this? If not, can you ask the user what he/she wants to do with it? Thanks!" The browser then either opens it (if the browser has been set up to open PDFs, for example in the "Applications" tab in Firefox), or present a dialog box to the user along the lines of "Do you want to open or save this file?"

    The header() directive looks something like this:

    PHP Code:
        header('Content-Description: File Transfer');
        
    header('Content-Type: application/octet-stream');
        
    header('Content-Disposition: attachment; filename='.$_GET['downloadfile']);
        
    header('Content-Transfer-Encoding: binary');
        
    header('Expires: 0');
        
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        
    header('Pragma: public');
        
    header('Content-Length: ' filesize($_GET['file']));

        
    readfile($_GET['file']); 

  • #3
    New Coder
    Join Date
    May 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, unfortunately I don't understand this. Only because I am VERY new to php, but it has to be done. When the user clicks on the option I want them to hit submit then, it opens.

    Not sure where to even put the code you suggested above. Thank you for your help though


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •