1
Good afternoon, everyone!!
I’m trying to implement a program where I have to get files. txt from a folder, open them and grab the contents of this file to store in the database, I am facing difficulty to open the file and view the content using variables in PHP.
<?php
$extensions = array('txt'); // image extensions
$result = array();
$directory = new DirectoryIterator('C:\Users\Fernando\Desktop\Imagens produto\APPLE'); // directory to scan
$dir = "C:/Users/Fernando/Desktop/Imagens produto/APPLE";
foreach ($directory as $fileinfo) {
if ($fileinfo->isFile()) {
$extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
if (in_array($extension, $extensions, $dir)) {
$result[] = $fileinfo->getFilename();
$conteudo =$dir."/".$fileinfo; //fiz uma gambiarra para juntar o diretorio com o nome do arquivo, para poder abrir em uma variavel com o caminho inteiro do arquivo
//echo $conteudo."<br>";
}
}
}
$a = open($conteudo);
echo $a."<br>";
/*while (!feof ($a)){
$x = fgets($a, 5120);
echo $x."<br>";
}
fclose($a);*/
//print_r($extensions);
?>
http://php.net/manual/en/function.file-get-contents.php
– bfavaretto
You used a thousand functions to call the extension in the file, and the
SplFileInfo
(that is returned in each iteration of theDiretoryIterator
) already has the method$file->getExtension()
. You can simplify your code.– Wallace Maxters