Your current code presents a number of problems regarding the use of variables, matrices, objects and HTML. The suggestion to resolve each of the issues is extensive and lacks the rest of the code you use until you reach the excerpt that is in your question.
Some of the visible problems in your question:
$file
is an object, but you’re going through the same as the matrix key $archive
;
Your cycle for()
would only work with the implementation you have, if the matrix $archive
had numerical keys;
You’re output from <tr></tr>
and only then do you output <td></td>
. As TD
has to be inside the TR
.
He’s working the matrix $archive
as if it had numeric keys which is what the variable $i
contains. (see paragraph 1);
You are concatenating $file->getBaseName()
after you close the TD
. It would have to be concatenated before the TD
be closed;
If there are no results, you will have a table with no contents;
List Files and Directories
From what I understand, you intend to list files and directories of a certain path to present to the user. Below follows 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:
Briefly, what is being done is:
Set the path to list in the variable $pathToList
:
$pathToList = "/caminho/para/destino/*";
Making use of the method DirectoryIterator
(English) we built an iteration of the directory:
$dir = new DirectoryIterator($pathToList);
For each input located, we perform the intended actions. In the example above we are listing the localized content by assigning an icon for 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.
Strange that
$archive = [$file];
, what is the intention? What shows avar_dump($file);
?– brasofilo
$file is an object? Why is $Archive = [$file]?
– abfurlan
$file are the FTP folders I want to organize.
– Vinícius
$i <= count($archive)
?– Beterraba