Appear Extension icon according to file extension (doc, xls, pdf)

0

I'm 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 wonder if it is possible for me to create a column on the left only that an icon appears per the extension. Ex:

If the file has a .pdf extension, display a PDF linked icon If the file has the .xls extension display an attached icon to Excel If the file has the .doc extension display an icon linked to Word If the file has a .txt extension with a TXT-connected icon

I do not know if there is a function that performs this process. Can you help me?

    
asked by anonymous 01.01.2018 / 23:15

1 answer

1

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 receives the path to a file as a parameter and returns the path of an image associated with that file (according to the icon). Then it could look 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 could 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']?>");
    
01.01.2018 / 23:49