Actions

 Language:
 RSS flow:


Tips, on files


Knowing the size of a file (in KB)

    To know the size of a file, you have to use the filesize function. Coupled with a small division by 1024, the function we will return the file size in kilobytes.
      $file = "Myfile.extention";
      if(file_exists($file)) {
        echo round(filesize($file)/1024);
      }
      

Count the number of lines of a file

    To count the number of lines of a file, you have to use count statement:
        $file = "myfile.txt";
        if(file_exists($file)) {
          $tab = file($file); 
          echo count($tab);
        }
      

List the files in a directory in alphabetical order

    To list the files in a directory in alphabetical order, use a table to sort. For that, one can use the small code:
        function alpha_sort($path) {
          // Table creation (to save files and folders)
          $files = array();
          $path = realpath($path) . DIRECTORY_SEPARATOR;
     
          if (is_dir($path)) {
             // Opening current path
            $handle = opendir($path);
            // List current files and directories
            while (($f = readdir($handle)) !== FALSE) {
                if ($f != '.' && $f != '..') {
                    array_push($files, $f);
                }
            }
     
            // Close current directory
            closedir($handle);
             // Sort the table
            natsort($files);
          }
     
          return $files;
        }
     
        // display sorted files and folders of current directory
        foreach(alpha_sort('.') as $f) {
          echo $f . '<br />';
        }
      

Knowing the date of last modification of a file

    For that, you have to use instruction:
        echo date ("Y/m/d H:i", filemtime("myfile.txt");
      
Go back