$archivos = scandir(".");
print_r($archivos);
$gestor_dir = opendir(".");
while ($nombre_fichero = readdir($gestor_dir)) {
$ficheros[] = $nombre_fichero;
}
print_r($ficheros);
recorrerCarpeta(".");
function recorrerCarpeta($carpeta) {
echo "Contenido de: " . $carpeta . "<br/>";
$archivos = scandir($carpeta);
array_shift($archivos);
array_shift($archivos);
foreach ($archivos as $a){
if (is_dir($a)){
recorrerCarpeta($carpeta."/".$a);
}
else{
echo $carpeta."/".$a."<br/>";
}
}
}
Etiqueta: ficheros
Funciones de archivo
Escribir en un fichero:
if (!$f = @fopen("pepe.txt", "w")) {
die("No he podido abrir el fichero: " . error_get_last()['message']);
}
for ($i = 1; $i <= 10; $i++) {
fwrite($f, "$i x 5=".(5*$i)."\r\n");
}
fclose($f);
if (!$f = @fopen("pepe.txt", "a")) {
die("No he podido abrir el fichero: " . error_get_last()['message']);
}
for ($i = 1; $i <= 10; $i++) {
fwrite($f, "$i x 7=".(7*$i)."\r\n");
}
fclose($f);
Leer un fichero:
$f = @fopen("pepe.txt", "r") or die("No he podido abrir el fichero");
//Lee una línea
while (!feof($f)) {
echo "<h2>".fgets($f)."</h2>";
}
fclose($f);
Leer directamente un fichero:
$cadena= file_get_contents("pepe.txt");
$tabla=file("pepe.txt");
echo $cadena;
print_r($tabla);