Web Development Exam  >  Web Development Videos  >  PHP for Absolute Beginners: From Novice to PHP Master  >  Beginner PHP Tutorial - 83 - File Handling: Listing Files Part 2

Beginner PHP Tutorial - 83 - File Handling: Listing Files Part 2 Video Lecture | PHP for Absolute Beginners: From Novice to PHP Master - Web Development

200 videos

FAQs on Beginner PHP Tutorial - 83 - File Handling: Listing Files Part 2 Video Lecture - PHP for Absolute Beginners: From Novice to PHP Master - Web Development

1. How can I list all the files in a directory using PHP?
Ans. To list all the files in a directory using PHP, you can use the opendir() function to open the directory, then use the readdir() function to read each file in the directory. Here's an example code snippet: ```php $directory = "path/to/directory"; if(is_dir($directory)){ if($dh = opendir($directory)){ while(($file = readdir($dh)) !== false){ if($file != '.' && $file != '..'){ echo $file . "<br>"; } } closedir($dh); } } ``` This code will list all the files in the specified directory, excluding the "." and ".." directories.
2. How can I display only specific file types from a directory using PHP?
Ans. If you want to display only specific file types from a directory using PHP, you can modify the code snippet mentioned in the previous question. Here's an example to display only files with the ".txt" extension: ```php $directory = "path/to/directory"; if(is_dir($directory)){ if($dh = opendir($directory)){ while(($file = readdir($dh)) !== false){ if($file != '.' && $file != '..' && pathinfo($file, PATHINFO_EXTENSION) == 'txt'){ echo $file . "<br>"; } } closedir($dh); } } ``` This code will only display files with the ".txt" extension from the specified directory.
3. How can I sort the listed files in alphabetical order using PHP?
Ans. To sort the listed files in alphabetical order using PHP, you can store the filenames in an array and then use the sort() function to sort the array. Here's an example code snippet: ```php $directory = "path/to/directory"; $files = array(); if(is_dir($directory)){ if($dh = opendir($directory)){ while(($file = readdir($dh)) !== false){ if($file != '.' && $file != '..'){ $files[] = $file; } } closedir($dh); } } sort($files); foreach($files as $file){ echo $file . "<br>"; } ``` This code will sort the filenames in alphabetical order before displaying them.
4. How can I list the files in a directory along with their file sizes using PHP?
Ans. To list the files in a directory along with their file sizes using PHP, you can modify the code snippet mentioned in the first question. Here's an example: ```php $directory = "path/to/directory"; if(is_dir($directory)){ if($dh = opendir($directory)){ while(($file = readdir($dh)) !== false){ if($file != '.' && $file != '..'){ $filePath = $directory . '/' . $file; echo $file . " - " . filesize($filePath) . " bytes<br>"; } } closedir($dh); } } ``` This code will display each filename along with its corresponding file size in bytes.
5. How can I list the files in a directory sorted by their last modified date using PHP?
Ans. To list the files in a directory sorted by their last modified date using PHP, you can modify the code snippet mentioned in the first question. Here's an example: ```php $directory = "path/to/directory"; $files = array(); if(is_dir($directory)){ if($dh = opendir($directory)){ while(($file = readdir($dh)) !== false){ if($file != '.' && $file != '..'){ $filePath = $directory . '/' . $file; $files[$file] = filemtime($filePath); } } closedir($dh); } } arsort($files); foreach($files as $file => $modifiedTime){ echo $file . " - " . date("F d Y H:i:s.", $modifiedTime) . "<br>"; } ``` This code will display each filename along with its corresponding last modified date, sorted in descending order.
Explore Courses for Web Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

ppt

,

Important questions

,

Beginner PHP Tutorial - 83 - File Handling: Listing Files Part 2 Video Lecture | PHP for Absolute Beginners: From Novice to PHP Master - Web Development

,

Semester Notes

,

study material

,

Beginner PHP Tutorial - 83 - File Handling: Listing Files Part 2 Video Lecture | PHP for Absolute Beginners: From Novice to PHP Master - Web Development

,

Objective type Questions

,

Exam

,

shortcuts and tricks

,

Beginner PHP Tutorial - 83 - File Handling: Listing Files Part 2 Video Lecture | PHP for Absolute Beginners: From Novice to PHP Master - Web Development

,

Viva Questions

,

video lectures

,

MCQs

,

Sample Paper

,

pdf

,

mock tests for examination

,

past year papers

,

Previous Year Questions with Solutions

,

Summary

,

Free

,

practice quizzes

,

Extra Questions

;