PHP: Vérifier si un élément est un répertoire

<html>
<head>
<title>Is the file a directory: is_dir()</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_dir( $f )?"":"not ")."a directory<br>";
}
?>
</body>
</html>
           
       

PHP: Vérifier si un fichier existe sinon le créer

<?php
  $myfile = "./test.txt";
  if (file_exists ($myfile)){
    $msg="File already exists. ";
  }else{
    $myfile = @fopen ($myfile, "w+") or die ("Couldn't create the file"  );
    $msg= "File created! " ;
    fclose ($myfile);
  }
  echo "$msg"; 
?>
           
       

PHP: Supprimer un fichier

<?php
   $file_delete = "home/meeta/my.php";
   
   if (unlink($file_delete)) {
      echo "The file was deleted successfully.", " ";
   } else {
      echo "The specified file could not be deleted. Please try again.", " ";
   }
?>
           
       

PHP: Vérifier si Le fichier est accessible en écriture

<html>
<head>
<title>Is the file writable: is_writable()</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_writable( $f )?"":"not ")."writable<br>";
}
?>
</body>
</html>
           
       

PHP: Trouver la date de modification d’un fichier

<html>
<head>
<title>File changed time</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 was changed on ".date( "D d M Y g:i A", filectime( $f ) )."<br>";
}
?>
</body>
</html>
           
       

PHP: Ajouter le contenu d’un fichier dans String

 
Its syntax is: string fgets (int filepointer, int length)
<?
$fh = fopen("data.txt", "r");
while (! feof($fh)) :
     $line = fgets($fh, 4096);
     print $line."<br>";
endwhile;
fclose($fh);
?>
  
  

PHP: Lire un caractère d’un fichier

 
Its syntax is: string fgetc (int filepointer)
<?
$fh = fopen("data.txt", "r");
while (! feof($fh)) :
     $char = fgetc($fh);
     print $char;
endwhile;
fclose($fh);
?>
  
  

PHP: Lire un fichier mot par mot

 
<?php
$fh = fopen('novel.txt','r') or die($php_errormsg);
while (! feof($fh)) {
    if ($s = fgets($fh)) {
        $words = preg_split('/s+/',$s,-1,PREG_SPLIT_NO_EMPTY);
    }
}
fclose($fh) or die($php_errormsg);
?>
  
  

PHP: Lire le contenu d’un répertoire

<?php
    $dir = opendir("./");
                
    while ($read_file = readdir($dir)) {
      echo "$read_file", " ";
    }
    closedir($dir);
?>
           
       

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;
?>