I have a module that lets employees upload files to the server for use by other employees. When another employee wants to see the a file, he clicks on a hyperlink. If the previously uploaded file is a PDF or text file, it opens up without a problem. If the file is a Word or Excel file, it opens as garbage.

The employees could right click and save as, but it would be more convenient if they could simply open the file.

Can anybody point me in the right direction?

Thanks,
Robkir

    what does you code look like for the others that work? and the others that dont? have you tried declaring content type in your header?

      @ the reason is the brower itself can read and show text and pdf formats, but it cant's read Docs and Excel sheets so it needed to use the associated program to open it.

        Hi anoopmail,
        Your solution is just the ticket. Thanks!

        robkir

          Hi Diaman,
          I was using a hyperlink to open the files. Anoopmail's header solution looks like it will work. Thanks
          robkir

            Looks like I spoke to soon...should have actually tried it out.

            I uploaded an excel spread sheet to the server and tested it as follows:

            header("Content-Description: File Transfer");

            header("Content-Type: application/ms-excel");
            header("Content-Disposition: attachment; filename=Book1.xls");

            I get the typical download box, but when I click okay it comes back with an error message saying it is in the wrong format. I also tried a doc on an ms-word file with the same result.

            How about docx and xlsx?

            Thanks,
            robkir

              Hi Brad,

              I send the files up with this code:

              <form method="post" enctype="multipart/form-data" name="form1" id="form1" 
                     action="upload_process.php">
                     <input name="ufile" type="file" id="ufile" size="75" /></td>
                      <input type="submit" name="Submit" value="Upload" />'.$error['ufile'].'</td>
              
              </form>
              

              upload_process.php looks like this

              // I add a unique identifier to the file name, so that if a person uploads a file by the name of quotes.pdf one week and another, unrelated quotes.pdf the next week, they won't overwrite each other. We do that by opening up the last record 
              
                $this_uploadedFileName="ID" . mysql_insert_id() ."_"; 
              
              $path= 'message_docs/'.$this_uploadedFileName.$HTTP_POST_FILES['ufile']['name'];
              if($ufile !=none)
              {
              if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
              {
              echo "<div align='center' style='cellpadding: 4px; width:350px; height: 200px; margin: 0 auto 0 auto; background-color: yellow;'>";
              echo "<font size='+1px'>File Transfer Was Successful</font><br />";
              echo "<strong><p class =\"text_reg\">File Name :</strong>".$HTTP_POST_FILES['ufile']['name']."<BR/>";
              echo "<strong><p class =\"text_reg\">File Re-Named :</strong>".$this_uploadedFileName.$HTTP_POST_FILES['ufile']['name']."<BR/>";
              echo "<strong>File Size :</strong>".$HTTP_POST_FILES['ufile']['size']."<BR/>";
              echo "<strong>File Type :</strong>".$HTTP_POST_FILES['ufile']['type']."<BR/>";
              
              echo "<p><a href='../updateConversations.php'>Return to Previous Page</a></p>";
              echo "</div>";
              }
              
              // else statement responds to user errors 
              

              I downloaded the excel file originally uploaded to the server and compared it to the local source. It opened up without a problem and contains the same data.

                Nothing seems wrong in the code. Try adding this line also

                header("Content-Transfer-Encoding: binary");
                

                nd are you sure you use readfile() to out[put the content? Post the final code here.

                  Originally I assumed I could just use a hyperlink to open the file (as I often do with the websites that I have files on). That didn't work. The readfile() function looks like it will solve the problem.

                  The basic code I think I will use is:

                  $file = 'excelfileOnServer.xlsx';
                  
                  if (file_exists($file)) {
                      header('Content-Description: File Transfer');
                      header('Content-Type: application/octet-stream');
                      header('Content-Disposition: attachment; filename='.basename($file));
                      header('Content-Transfer-Encoding: binary');
                      header('Expires: 0');
                      header('Cache-Control: must-revalidate');
                      header('Pragma: public');
                      header('Content-Length: ' . filesize($file));
                      ob_clean();
                      flush();
                      readfile($file);
                      exit;
                  }
                  

                  As a sidenote, readfile() does not like spaces in the file name.

                  I appreciated your advice. Thanks.

                    Write a Reply...