IF and ELSE if the file is PDF or DOCX

Asked

Viewed 49 times

-1

I have a system, where monthly files (docx and pdf) are registered, for me to view docx files in the browser I had to use a iframe, and now I can see the docx, but I can no longer view the PDF...

I was thinking of using a IF and ELSE, but I don’t know how to differentiate the files...

To better understand: If the file is DOCX open the file that way, if not, that way...

I redirect the files to the page (see-file.php) to be opened.

Where files are sent:

echo $cont['relatorio_educacao_fisica'] != "" ? "<td><a style='color: Blue' href='ver-arquivo.php?documento=uploads/uploadsed/{$cont['relatorio_educacao_fisica']}' title='{$cont['relatorio_educacao_fisica']}' target='_blank'><img src='images/icon/bola.png' style='width:40px; height:40px;cursor:pointer;'></a></td>" : "<td></td>";


                echo $cont['relatorio_enfermagem'] != "" ? "<td><a style='color: Blue' href='ver-arquivo.php?documento=uploads/uploadsenf/{$cont['relatorio_enfermagem']}' title='{$cont['relatorio_enfermagem']}' target='_blank'><img src='images/icon/enf.png' style='width:40px; height:40px;cursor:pointer;'></a></td>" : "<td></td>";

                     echo $cont['relatorio_nutricao'] != "" ? "<td><a style='color: Blue' href='ver-arquivo.php?documento=uploads/uploadsnut/{$cont['relatorio_nutricao']}' title='{$cont['relatorio_nutricao']}' target='_blank'><img src='images/icon/nut.png' style='width:40px; height:40px;cursor:pointer;'></a></td>" : "<td></td>";

                       echo $cont['relatorio_pedagogia'] != "" ? "<td><a style='color: Blue' href='ver-arquivo.php?documento=uploads/uploadsped/{$cont['relatorio_pedagogia']}' title='{$cont['relatorio_pedagogia']}' target='_blank'><img src='images/icon/ped.png' style='width:45px; height:45px;cursor:pointer;'></a></td>" : "<td></td>";

                          echo $cont['relatorio_terapia_ocupacional'] != "" ? "<td><a style='color: Blue' href='ver-arquivo.php?documento=uploads/uploadster/{$cont['relatorio_terapia_ocupacional']}' title='{$cont['relatorio_terapia_ocupacional']}' target='_blank'><img src='images/icon/ter.png' style='width:45px; height:45px;cursor:pointer;'></a></td>" : "<td></td>";

                         echo $cont['relatorio_servicos_sociais'] != "" ? "<td><a style='color: Blue' href='ver-arquivo.php?documento=uploads/uploadser/{$cont['relatorio_servicos_sociais']}' title='{$cont['relatorio_servicos_sociais']}' target='_blank'><img src='images/icon/ser.png' style='width:45px; height:45px;cursor:pointer;'></a></td>" : "<td></td>"; 

Code where I view docx files: (view-file.php)

<iframe class="arquivos" src="https://view.officeapps.live.com/op/embed.aspx?src=https://www.meusite.com/<?php echo urlencode($_GET['documento']); ?>"></iframe>

Code of how I was able to view the pdf files:

<a href='uploads/uploadsenf/".$cont['relatorio_enfermagem']."'target='_blank'>".$cont['relatorio_enfermagem']."</a>

Does anyone know how I can do it with IF and ELSE, or some other function?

  • where these files are saved?

  • The file path is saved in the database and the files themselves in directory

  • Come on, what I would do is: create a white list containing the allowed extensions and after that use one of the following functions: https://www.php.net/manual/en/function.finfo.php - https://www.php.net/manual/en/function.mime-content-type.php

  • I didn’t give you the ready IF code yesterday in chat? This https://pastebin.com/raw/JUrZZfRN

  • Eai William, it didn’t work, then you had to leave

1 answer

1

You can use the function pathinfo(), with it you will have some information about the file. And one of them is the extension. You can get more information about this function in this link.

// Cria um array contendo os caminhos dos arquivos.
// Uma dica é que você pode aproveitar e colocar os icones para exibi-los dentro do laço
$data = [
    [
        'path' => 'relatorio_educacao_fisica',
        'icon' => 'images/icon/bola.png',
    ],
    [
        'path' => 'relatorio_enfermagem',
        'icon' => 'images/icon/enf.png',
    ],
    [
        'path' => 'relatorio_nutricao',
        'icon' => 'images/icon/nut.png',
    ]
];


foreach ($data as $value) {
    // Verifica se o caminho está "setado"
    if (isset($value['path'])) {
        // Pega a extensão do arquivo
        $extension = pathinfo($value['path'], PATHINFO_EXTENSION);
        if ($extension == 'pdf') {
            // Exibe o link com o icone correto para cada arquivo
            echo "<td><a style='color: Blue' href='ver-arquivo.php?documento=uploads/uploadsenf/{$value['path']}' title='{$value['path']}' target='_blank'><img src='{$value['icon']}' style='width:40px; height:40px;cursor:pointer;'></a></td>";
        } else {
            // Exibe seu iframe
            echo '<iframe class="arquivos" src="https://view.officeapps.live.com/op/embed.aspx?src=https://www.meusite.com/' . urlencode($value['path']) . '"></iframe>';
        }
    } else {
        echo '<td></td>';
    }
}

I tried to bring my example as close as possible to your code, but I need you to understand what the code does, so don’t just try to copy and paste the code and hope it works.

  • a foreach traversing directories.

  • 1

    What do you mean 6 directories? Please try to put the example of the code, with this it is easier to help you.

  • Have 6 fields for file entries, each field has its directory.

  • Arthur’s idea is a good one. But if possible put the code there, it is easier for us to help you.

  • I have, I’m new to programming and I get a little confused....

  • I will edit my answer to try to solve it in the best way.

  • On hold.....

Show 2 more comments

Browser other questions tagged

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