Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function readLine($lineNumber, $fileName) {
- /**
- * @param int $lineNumber The line number you want to read from the file.
- * @param string $fileName The string representing the file you wish to open.
- *
- * @return string The contents of the line in the file.
- */
- $file = file($fileName);
- return $file[$lineNumber-1];
- }
- function readLineFrom($lineNumber, $fileNames) {
- /**
- * @param int $lineNumber The line number you want to read from the file.
- * @param string|array $files Either a string for the file you want to open, or an array of filenames.
- *
- * @return array An associative array of the filename to the contents of the line.
- */
- $lines = array();
- if(is_array($fileNames)) {
- foreach($fileNames as $fileName) {
- $lines[$fileName] = readLine($lineNumber,$fileName);
- }
- } else {
- $lines[$fileNames] = readLine($fileNames);
- }
- return $lines;
- }
- function getFileNamesFromDirectory($directory = '.') {
- /**
- * @param string $directory The path to directory you'd like to get filenames from. Defaults to current directory.
- *
- * @return array An array of filenames. Empty if there are no files.
- */
- $fileNames = array();
- if ($handle = opendir($directory)) {
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != "..") {
- $fileNames[] = $file;
- }
- }
- closedir($handle);
- }
- return $fileNames;
- }
- echo "<pre>";
- print_r(
- readLineFrom(5, getFileNamesFromDirectory('/var/www/'))
- );
- echo "</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment