<?php $dir = opendir("./"); while ($read_file = readdir($dir)) { echo "$read_file", " "; } closedir($dir); ?> |
Fichiers et Répertoires
PHP: Trouver le nombre de ligne d’un fichier
<?php $lines = 0; if ($fh = fopen('data.txt','r')) { while (! feof($fh)) { if (fgets($fh)) { $lines++; } } } print $lines; ?> |
PHP: Véfirier si élément est un fichier
<html> <head> <title>Is it a file: is_file()</title> </head> <body> <?php $file = "test.txt"; outputFileTestInfo( $file ); function outputFileTestInfo( $f ){ if ( ! file_exists( $f ) ){ print "$f does not exist<BR>"; return; } print "$f is ".(is_file( $f )?"":"not ")."a file<br>"; } ?> </body> </html> |
PHP: Vérifier si un fichier est exécutable
<html> <head> <title>Is the file executable: is_executable</title> </head> <body> <?php $file = "test.txt"; outputFileTestInfo( $file ); function outputFileTestInfo( $f ){ if ( ! file_exists( $f ) ){ print "$f does not exist<BR>"; return; } print "$f is ".(is_executable( $f )?"":"not ")."executable<br>"; } ?> </body> </html> |
PHP: Afficher le nom du répertoire
<?php $path = "/home/www/data/users.txt"; $dirname = dirname($path); // $dirname contains "/home/www/data" ?> |
PHP: Afficher l’espace total d’un disque
<?php $systempartitions = array("/", "/home","/usr", "/www"); foreach ($systempartitions as $partition) { $totalSpace = disk_total_space($partition) / 1048576; $usedSpace = $totalSpace - disk_free_space($partition) / 1048576; echo "Partition: $partition (Allocated: $totalSpace MB. Used: $usedSpace MB.) <BR>"; } ?> |
PHP: Vérifier si le fichier est lisible
<html> <head> <title>Is the file readable: is_readable()</title> </head> <body> <?php $file = "test.txt"; outputFileTestInfo( $file ); function outputFileTestInfo( $f ){ if ( ! file_exists( $f ) ){ print "$f does not exist<BR>"; return; } print "$f is ".(is_readable( $f )?"":"not ")."readable<br>"; } ?> </body> </html> |
PHP: Déplacer un fichier
<?php $source = "./test.txt"; $destination = "./copy.txt"; if (rename($source, $destination)) { echo "The file was moved successfully.", " "; } else { echo "The specified file could not be moved. Please try again.", " "; } ?> |
PHP: Lire les lignes d’un fichier
<?php $fh = fopen('orders.txt','r') or die($php_errormsg); while (! feof($fh)) { $s = fgets($fh,256); process_order($s); } fclose($fh) or die($php_errormsg); ?> |
PHP: Fermer un fichier
<? $filename = 'data.txt'; $handle = fopen($filename, "rb"); $contents = fread($handle, filesize($filename)); fclose($handle); print $contents; ?> |