Open files downloaded from PHP

3

I'm trying to open files with this function. For files *.pdf and *.txt works fine, but if the file is *.doc or *.odt when downloading and opening it, strange characters appear.

$filepath = '/var/download/prueba.doc';
    if(file_exists($filepath)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length:'.filesize($filepath));
            flush(); // Flush system output buffer
            readfile($filepath);
            exit();
        }

What am I doing wrong? Is there any way to open any type of file once downloaded ?. I do not need to open them online, I need to be able to download them and once downloaded, I can read them on my PC.

    
asked by Lorenzo Martín 04.09.2018 в 16:45
source

3 answers

1

Your code seems correct, so the problem you suffer may be due to two reasons:

  • The PHP file is encoded in UTF-8 with BOM .
  • You have left a blank space, return the car, tabulation, special character not printable, etc. before opening the <?php tag.

In pdf and txt documents there is no problem if there is something at the beginning, but the documents doc, docx, odt, etc. (the last two are actually a compressed zip file) do not allow any addition, opening as text once loaded the application, showing its raw content.

I recommend that you check both to solve your problem.

    
answered by 04.09.2018 / 22:30
source
0

To be able to open a document like word or excel online you must have a library, here it explains how it would be to read an excell with php:

link

I hope I'll help you with something.

    
answered by 04.09.2018 в 17:36
0

As mentioned in the comment you would have to use a vizor to see the documents, in the following example you can use the google:

<iframe src="https://docs.google.com/gview?url=https://github.com/chrahunt/docx/blob/master/spec/fixtures/basic.docx?raw=true&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>

I hope you could have served or at least given a guide.

    
answered by 04.09.2018 в 17:58