← PHP

Ajuda a listar pastas

Lida 4129 vezes

Offline

HelderT 
Membro
Mensagens 4 Gostos 0
Troféus totais: 14
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Level 3 Level 2 Level 1 First Post Karma Sixth year Anniversary Fifth year Anniversary

Boas eu tenho o seguinte código:

Código: (php) [Seleccione]
<html>
<head>
<title>Listar Pastas</title>
</head>

<body>
<?php
$count=1//Variavel contadora

$diretorio 'C:/xampp/htdocs/substituir/Teste'//Diretorio onde estao as pastas
$abrir opendir($diretorio); // Abrir esse mesmo diretorio

//Iniciar a tabela
echo '<table width="100%" border="1">';
echo '<tr> <td width="50%" height="40px"><div align="center">NOME</div></td> <td width="50%"><div align="center">NUMERO</div></td> </tr>';

while($ler=readdir($abrir)){ //enquanto existir pastas para ler...
if($ler!='.' && $ler!='..'){ //if para nao mostrar as pastas ocultas com o nome de '.' e '..'
echo '<tr><td width="50%" height="25px"><div align="center"><p>'.$ler.'</p></div></td>';
echo '<td width="50%" height="25px"><div align="center"><p>'.$count.'</p></div></td></tr>';
$count=$count+1;
#FIM DO IF
#FIM DO WHILE

//Finalizar a tabela
echo "</table>";
?>

</body>
</html>

Isto resulta-me no seguinte output:


A minha dúvida agora é como faço para só mostrar as pastas como o nome " *_copiar ". Se alguém me poder dar uma pequena ajuda agradeço.

Comprimentos.
Offline

Bruno Gaspar 
Membro
Mensagens 242 Gostos 1
Feedback +3

Troféus totais: 18
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 3 Level 2 Level 1 100 Posts 50 Posts 10 Posts

Vê se ajuda:
Código: [Seleccione]
<?php
define
('DS'DIRECTORY_SEPARATOR);

$directorio 'teste';

$teste array_map(function($file)
{
return basename($file);
}, (array) 
glob($directorio DS '*_copiar'GLOB_NOSORT));
?>


<table width="100%" border="1">
<?php
$contador 1;
foreach ($teste as $t)
{
?>

<tr>
<td width="50%" height="25px"><div align="center"><?php echo $t?></div></td>
<td><?php echo $contador++; ?></td>
</tr>
<?php
}
?>

</table>
Offline

HelderT 
Membro
Mensagens 4 Gostos 0
Troféus totais: 14
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Level 3 Level 2 Level 1 First Post Karma Sixth year Anniversary Fifth year Anniversary

Ajudou bastante, muito OBRIGADO!!
Já agora dá para mostrar o nome da pasta sem o "_copiar"?

Comprimentos.
Offline

Bruno Gaspar 
Membro
Mensagens 242 Gostos 1
Feedback +3

Troféus totais: 18
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 3 Level 2 Level 1 100 Posts 50 Posts 10 Posts

Sim dá:
Código: [Seleccione]
$teste = array_map(function($file)
{
return str_replace('_copiar', '', basename($file));
}, (array) glob($directorio . DS . '*_copiar', GLOB_NOSORT));
Offline

HelderT 
Membro
Mensagens 4 Gostos 0
Troféus totais: 14
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Level 3 Level 2 Level 1 First Post Karma Sixth year Anniversary Fifth year Anniversary

MUITO OBRIGADO PELA TUA AJUDA!!
 :obrigado:

Comprimentos.

P.S - Podem fechar o tópico.  :grin: