Appear extension icon as per file extension (doc, XLS, pdf)

I am having a demand for a php system.

I am listing documents from a certain directory that is on the server in my html page

  <div class="tab-pane" id="docs">


                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Documento</th>


                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $result = mysql_query("SELECT * FROM gr_documento 
                                                                                                                        WHERE id_entidade_documento = ". $_GET['id']."
                                                                                                                        AND tipo = 1");
                                    while ($row = mysql_fetch_array($result)) {                               
                                    ?>

                                    <tr>
                                                                                <td><a href="/upload/docs/<?php echo $row['novo_nome']?>" target=“_blank” ><?php echo $row['nome_documento']?></a></td>

                                    </tr>

                                     <?php
                                     }
                                     ?>

                                </tbody>
                            </table>
                        </div>


                            </div>

I would like to know if it is possible I create a column on the left only appearing an icon according to the extension. Ex:

If the file has the extension .pdf display an icon of attached to PDF If the file has the extension .XLS display an icon attached to Excel If the file has the extension .doc display an icon attached to Word If the file has the extension .txt display an icon of connected to txt

I don't know if there is any function that performs this process. Can you help me ?

Author: Robson Freitas, 2018-01-01

1 answers

You can create a function to return the image associated with the type of a file received as a parameter. Explaining better, you create a function that takes as a parameter the path to a file and returns the path of an image associated with that file (according to the icon). So it could be something like this:

function imagemMimeType($caminhoArquivo){
     $tipos = [
    'text/plain' => 'imagens/icone1.png', 
    'image/png' => 'imagens/icone2.png', 
    'image/jpeg' => 'imagens/icone3.png', 
    'application/pdf' => 'imagens/icone4.png'];

     //veja mais tipos em http://php.net/manual/pt_BR/function.mime-content-type.php

     $tipo = mime_content_type ($caminhoArquivo);
     return $tipos[$tipo];
}

And within your while loop you would be able to figure out which icon should be associated with your files. Something like this:

//não necessariamente precisa de extensão
//echo imagemMimeType('caminho_do_arquivo.entensao');
$caminhoIcone = imagemMimeType("/upload/docs/<?php echo $row['novo_nome']?>");
 1
Author: Juven_v, 2018-01-01 22:49:31