How to check if a file exists in several different folders?

Asked

Viewed 3,112 times

4

I need a PHP script that checks if a file exists in a folder, if it doesn’t exist it checks in the next one and so on... I don’t play much with PHP so I’m a little lost.

I’ve tried to do with switch also but I could not.

<?php       
#checagem da existencia do arquivo em varias pastas diferentes

$imagem = foto.jpg;
$filename = "img/$imagem";
$filename_dois = "img-2/$imagem";
$filename_tres = "img-3/$imagem";
$filename_quatro = "img-4/$imagem";
$filename_cinco = "img-5/$imagem";

if (file_exists($filename)) {echo "<a href='img/$imagem' rel='shadowbox[cart]'>  <img src='timthumb.php?src=img/$imagem_da_certidao&h=150&w=200' /> </a>";} else
{(file_exists($filename_dois)) {echo "<a href='img-2/$imagem' rel='shadowbox[cart]'>  <img src='timthumb.php?src=img-2/$imagem&h=150&w=200' /> </a>";}}
else
{(file_exists($filename_tres)) {echo "<a href='img-3/$imagem' rel='shadowbox[cart]'>  <img src='timthumb.php?src=img-3/$imagem&h=150&w=200' /> </a>";}}
else
{(file_exists($filename_quatro)) {echo "<a href='img-4/$imagem' rel='shadowbox[cart]'>  <img src='timthumb.php?src=img-4/$imagem&h=150&w=200' /> </a>";}}
else
{(file_exists($filename_cinco)) {echo "<a href='img-5/$imagem' rel='shadowbox[cart]'>  <img src='timthumb.php?src=img-5/$imagem&h=150&w=200' /> </a>";}}
?>

3 answers

2


Do so:

So it is more friendly to add new folders, then put the IMG and image link where it is indicated.

<?php
// nome do arquivo
$imagem = "foto.jpg";

// aqui você pode adicionar várias pastas
$pastas = array(
    "img"=>$imagem, 
    "img-2"=>$imagem
    // ...
);

// percorre todas as pastas
foreach($pastas as $pasta => $imagem){
    // verifica se o arquivo existe
    if (file_exists($pasta . DIRECTORY_SEPARATOR . $imagem)) {
        // insira sua imagem aqui
        print $pasta . DIRECTORY_SEPARATOR . $imagem;
    }
}
?>  
  • More than a year later, the code has been very useful. So much so that I adopted this scheme for another adaptation to the front end that I did. The problem is that there is the same file in the declared folders, how to make it stop going through the folders after finding the file declared in $imagem? Currently I put the do involving all the function you gave me. Thank you.

1

<?php
$arquivo = "foto.jpg";
$diretorios = array('img', 'img-2', 'img-3', 'img-4', 'img-5');

foreach ($diretorios as $dir)
{
    if (file_exists("{$_SERVER['DOCUMENT_ROOT']}/{$dir}/{$arquivo}"))
    {
        echo "<a href='{$dir}/{$arquivo}' rel='shadowbox[cart]'><img src='timthumb.php?src={$dir}/{$arquivo}&h=150&w=200' /></a>";
        break;
    }
}

1

I created this function to check recursively from a base directory, how often there is a file and what directory it is in.. Despite the weather, see if it helps!

function listarArquivos($diretorio, $nomeArquivo) {
    $encontrados = "";
    $ponteiro = opendir($diretorio);

    while ($nome_itens = readdir($ponteiro)) {
        $itens[] = $nome_itens;
    }

    sort($itens);

    foreach ($itens as $listar) {
        if ($listar != "." && $listar != "..") {
            if (is_dir($diretorio . '/' . $listar)) {
                $encontrados .= listarArquivos($diretorio . '/' . $listar, $nomeArquivo);
            } else {
                if (preg_match('/' . $nomeArquivo . '/i', $listar)) {
                    $encontrados .= $diretorio . '/' . $listar . " <br> ";
                }
            }
        }
    }

    return $encontrados;
}

Browser other questions tagged

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