To better understand the problem, you can add a debugger to your code this way.
<?php
foreach(new DirectoryIterator('C:\xampp\htdocs\testtoclassificados\log') as $fileInfo){
echo "<pre>"; print_r($fileInfo); echo "</pre>";
/*$fp = fopen($fileInfo,"r");
$texto = fread($fp, filesize($fileInfo));
if($fileInfo->isDot())continue;
$arqName = $fileInfo->getFilename();
// $handle = $fileInfo->fopen("c:\\data\\info.txt", "r");*/
}
?>
Use print_r to print on the screen the instance of the Directoryiterator class returned by each interaction of your foreach loop. Each printable object is a file or directory within the log directory. You are using the fileInfo object instance as the path for the file and in reality it is an object instance with methods and attributes. Follow the documentation examples Directoryiterator Then remove the comment I added and check which last file or directory the php code displays the error message. Make sure you are not trying to use fopen in a directory. You have to validate the condition for fopen to read only files.
<?php
foreach(new DirectoryIterator('C:\xampp\htdocs\testtoclassificados\log') as $fileInfo){
//Se for . ou .. pular para a próxima interação do laço.
if($fileInfo->isDot())continue;
if($fileInfo->isFile()){
//DirectoryIterator::getPathname — Retorna o caminho e o nome
//do arquivo do elemento atual do diretório
$arquivo = $fileInfo->getPathname();
// Abre para leitura mas antes verifica se o arquivo
// tem permissão de leitura.
if($fileInfo->isReadable() == true){
$fp = fopen($arquivo,"r");
$texto = fread($fp, filesize($arquivo));
echo $texto."<br/><br/>";
fclose($fp);
}else echo "O arquivo ".$arquivo." não possuí permissão de leitura.";
}
}
?>
To make a file listing with links and open in new tab do so :
<?php
foreach(new DirectoryIterator('C:\xampp\htdocs\testtoclassificados\log') as $fileInfo){
//Se for . ou .. pular para a próxima interação do laço.
if($fileInfo->isDot())continue;
if($fileInfo->isFile()){
//DirectoryIterator::getPathname — Retorna o caminho e o nome
//do arquivo do elemento atual do diretório
$arquivo = $fileInfo->getPathname();
// Abre para leitura mas antes verifica se o arquivo
// tem permissão de leitura.
if($fileInfo->isReadable() == true){
$fp = fopen($arquivo,"r");
$texto = fread($fp, filesize($arquivo));
fclose($fp);
// A variavel $arquivo recebeu o valor do método contido
//dentro da intância da classe DirectoryInterator.
//Dentro desse objeto existe o método que retorna o caminho
//completo até o arquivo, como mostrado abaixo
//$fileInfo->getPathname(), portanto a variavel $arquivo possuí
//o path completo uma vez que definida acima.
$urlLeitor = "http://localhost/leitor.php?arquivo=".$fileInfo->getFilename();
echo"<li><a href='".$urlLeitor."' target='_blank'>".$texto."</a>
<br/></li>";
}else echo "O arquivo ".$arquivo." não possuí permissão de leitura.";
}
}
?>
Ricardo checks the permissions and user of the folder and files contained within testtoclassified. The error in question refers to this.
– Rafael Salomão
Then add this logic after your foreach if ($fileinfo->isFile()) to make sure it’s a file before reading with fopen. The unveiled eye is what I realized.
– Rafael Salomão
Fatal error: Uncaught Error: Call to a member function isFile() on null in C:\xampp\htdocs\testtoclassificados\
this message displays after I put isFile shows that it is null– Ricardo Mendes
I put if ($fileinfo->isFile()) no comments, and you in your code used $fileInfo with uppercase letter in info checks there in your code.
– Rafael Salomão
Just one question, why use fopen inside the iterator? The variable text seems to serve no purpose
$texto
.– Guilherme Nascimento
Because previously he was printing a piece of decoy text on the screen. I don’t know if he’ll keep it!
– Rafael Salomão