Your current code has a number of issues with using variables, arrays, objects, and HTML. The suggestion to solve each of the subjects is extensive and lacks the rest of the code that you use until you reach the excerpt that appears in your question.
Some of the problems visible in your question:
$file
is an object, but you are passing it as the key of the array $archive
;
You are outputting for()
and only then do you output $archive
. The <tr></tr>
must be within <td></td>
.
The TD
array is working as if it has numeric keys which is what the TR
contains. (see point 1);
You are concatenating $archive
after closing $i
. It would have to be concatenated before the $file->getBaseName()
is closed;
If there are no results, you will have a table without content;
List Files and Directories
As far as I understand, you want to list files and directories of a particular path to display to the user. Below is a working example that illustrates this:
<?php
$outputHtml = '';
$pathToList = dirname(__FILE__); // caminho a listar
$dir = new DirectoryIterator($pathToList); // Iterar o caminho
// por cada entrada encontrada
foreach ($dir as $fileinfo) {
// se não for "." ou ".."
if (!$fileinfo->isDot()) {
/* Verificar se é directoria ou ficheiro
* para definir o icon a utilizar
*/
if ($fileinfo->isDir()) {
$icon = 'https://cdn2.iconfinder.com/data/icons/snipicons/5000/folder-close-32.png';
}
else {
$icon = 'https://cdn2.iconfinder.com/data/icons/snipicons/500/file-32.png';
}
/* actualizar a variável de output com o HTML
* referente a este ficheiro/directoria
*/
$outputHtml.= '
<div>
<a href="index.php?path='.$fileinfo->getPathname().'">
<img src="'.$icon.'" width="32px" height="32px"/> '.$fileinfo->getFilename().'
</a>
</div>';
}
}
/* Apresentar a listagem ou se não temos
* resultados apresentar uma mensagem.
*/
if (!empty($outputHtml)) {
echo $outputHtml;
}
else {
echo '<p>Não foram encontrados ficheiros ou directorias para o caminho indicado!</p>';
}
?>
The code above will give you a listing similar to the screenshot below:
In short, what is being done is:
Define the path to list in the TD
variable:
$pathToList = "/caminho/para/destino/*";
Using the method TD
(English) we have built an iteration of the directory:
$dir = new DirectoryIterator($pathToList);
For each localized entry, we perform the desired actions. In the example above we are listing localized content by assigning an icon to when it is a file and another icon for when it is a directory.
foreach ($dir as $fileinfo) {
...
}
We present the result to the user if we have found something, if not, we present a message.