Appear Icone from the extension as per the file extension (doc, xls, pdf)

Asked

Viewed 195 times

0

I’m on a demand for a php system.

I am listing documents from a particular 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 to create a column on the left only by appearing an icon according to the extension. Ex:

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

I don’t know if there are any functions that perform this process. They can help me ?

1 answer

1


You can create a function to return the image associated with the type of a file received as parameter. Explaining better, you create a function that takes as 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/en/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']?>");
  • Thanks for the reply, I will perform a test :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.