-1

The intent of this is to have users submit applications to the client and for the client to review their information and select the best application. I am trying to display a document from a file server onto the php web page. MySQL code holds the path of the file and I'm using readfile(). I thought maybe it was the encoding or maybe it was the file type but nothing I do seems to be working. I tried using a different filetype and I tried using mb_convert_encoding to modify the encoding.

    <?php foreach ($applications as $application) : ?>        <p> Name: <?php echo $application['fname']; ?> <?php echo $application['mname']; ?> <?php echo $application['lname']; ?><br /></p>
    <?php  

    readfile ($file); ?>

   <?php endforeach; ?> 

Here's an example of the symbols that are displaying:

PK!���Ʌ)[Content_Types].xml �(�̕MK�@���!�U�m+�H��8j� ^��I���N�������Vۊ�@23���0;� �F'sQ9��N�f X�re'{=��YQ�\hg!c+�l�??�VbB�6fl��o8�r F��y�)\0�5L��ML�w��+.�E���R��{wP����~I�k�:��N,�2&��J �8����Kk�Re����J|�C��]�D� *�d(> CY|�B�s'g�*��28]Q( M}�惓#��贉���&9��̫�!�ap>v�iDK=������p���LD\i�ǟ�Zw�= R�)��;0~>�'� ���X��1�H�������fI����v �8���,�[~��oI���A��s��]�#�� �������PK!�U~��_rels/.rels �(���MK1���!̽;�"��^D�Md�C2��������(�.Ե�3y��3C֛��+�4xW��(A������yX܂JB���Wp����b��#InJ�����E�b�=[J���M�%���a �B�,o0�f@=a��� n�����o�A��;�N�

The file I'm trying to read is .doc file.

The Onin
  • 5,068
  • 2
  • 38
  • 55
  • What type of file is it? – aynber Feb 24 '20 at 17:04
  • `readfile()` doesn't work that way. It works with output buffers. Reply is complex to write as it relies on many factors. Please see this topic: https://stackoverflow.com/questions/2028898/php-output-file-on-disk-to-browser It's the same thing except it uses `fopen` instead. – user8555937 Feb 24 '20 at 17:05
  • OP, what kind of data are you storing in the file? – The Onin Feb 24 '20 at 17:12
  • It looks like an Office Open XML format (docm/docx, pptm/pptx, xlsx/xlsm), which means it's compressed. You're not going to be able to use `readfile` in this straight-forward way. See https://en.wikipedia.org/wiki/Office_Open_XML_file_formats for how they're built. – aynber Feb 24 '20 at 17:13
  • @NinoŠkopac there is only text in the file it's a .doc file and I've tried using a .docx file. – IncliningTitan Feb 24 '20 at 17:34
  • 1
    Possible duplicates of https://stackoverflow.com/questions/7358637/reading-doc-file-in-php and https://stackoverflow.com/questions/188452/reading-writing-a-ms-word-file-in-php and https://stackoverflow.com/questions/19503653/how-to-extract-text-from-word-file-doc-docx-xlsx-pptx-php – aynber Feb 24 '20 at 17:41
  • Like @aynber said, doc(x) files aren't plaintext files. You should've mentioned it's a doc(x) file in your question. – The Onin Feb 24 '20 at 18:00
  • Does this answer your question? [Reading DOC file in php](https://stackoverflow.com/questions/7358637/reading-doc-file-in-php) – The Onin Feb 24 '20 at 18:02

1 Answers1

0

$file seems undefined; assuming $application holds all the application data you want to display to the client, then you should be doing:

<?php readfile($application['file']); ?>

If this isn't working then you aren't using the correct path - try debugging like this:

<?php var_dump($application['file'], file_exists($application['file'])); ?>
The Onin
  • 5,068
  • 2
  • 38
  • 55
  • Readfile reads data in binary mode and works with output buffers instead. Please check the docs: https://www.php.net/readfile – user8555937 Feb 24 '20 at 17:08
  • @user8555937 Sure it writes to the output buffer, but so does _every_ function that outputs instead of returns and every outputting function (print, echo, var_dump, print_r, var_export...) behaves in the same way. Output buffering has nothing to do with this post. – The Onin Feb 24 '20 at 17:11