w3resource

PHP File Handling: Exercises, Practice, Solutions

PHP File Handling: Exercises Practice Solution [ 18 exercises with solution ]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

The following exercises cover various aspects of PHP File Handling. They will practice working with files, directories, file permissions, and file formats like text, CSV, JSON, XML, and binary.

1. Write a PHP program to read and display the contents of a text file.
Click me to see the sample solution

2. Write a PHP function that takes a file path as input and checks if the file exists.
Click me to see the sample solution

3. Write a PHP script to count the number of lines in a text file.
Click me to see the sample solution

4. Write a PHP function to write a string to a file.
Click me to see the sample solution

5. Write a PHP program that reads a CSV file and displays the data in a tabular format.
Click me to see the sample solution

6. Write a PHP script to rename a file.
Click me to see the sample solution

7. Write a PHP function that copies a file from one location to another.
Click me to see the sample solution

8. Write a PHP program that deletes a specific file from the server.
Click me to see the sample solution

9. Write a PHP function that checks the file size and displays it in human-readable format (KB, MB, GB).
Click me to see the sample solution

10. Write a PHP program that checks if a file is writable and displays appropriate messages.
Click me to see the sample solution

11. Write a PHP program that appends a string to an existing file.
Click me to see the sample solution

12. Write a PHP script to check the file extension and display appropriate messages for different file types.
Click me to see the sample solution

13. Write a PHP function to check if a directory exists and create it if it doesn't.
Click me to see the sample solution

14. Write a PHP program that searches for a specific word in a text file and displays the line number(s) where it occurs.
Click me to see the sample solution

15. Write a PHP program that reads and displays the contents of a binary file.
Click me to see the sample solution

16. Write a PHP program that reads an XML file and extracts specific data from it.
Click me to see the sample solution

17. Write a PHP function that reads a JSON file and returns the decoded data as an associative array.
Click me to see the sample solution

18. Write a PHP function to calculate the MD5 checksum of a file.
Click me to see the sample solution

PHP Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.