w3resource

PHP File Handling: Exercises, Practice, Solutions


This resource offers a total of 90 PHP File Handling problems for practice. It includes 18 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

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


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. Read and display contents of a text file

Write a PHP program to read and display the contents of a text file.

Click me to see the sample solution


2. Check if the file exists

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. Count the number of lines in a text file

Write a PHP script to count the number of lines in a text file.

Click me to see the sample solution


4. Write a string to a file

Write a PHP function to write a string to a file.

Click me to see the sample solution


5. CSV File to Table

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. Rename a File

Write a PHP script to rename a file.

Click me to see the sample solution


7. Copy a File

Write a PHP function that copies a file from one location to another.

Click me to see the sample solution


8. Delete a File

Write a PHP program that deletes a specific file from the server.

Click me to see the sample solution


9. Human-Readable File Size

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. Check if File is Writable

Write a PHP program that checks if a file is writable and displays appropriate messages.

Click me to see the sample solution


11. Append a String to a File

Write a PHP program that appends a string to an existing file.

Click me to see the sample solution


12. Check File Extension and Display Messages

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. Check/Create Directory if Not Exists

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. Search for Specific Word in Text File

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. Read and Display Binary File Contents

Write a PHP program that reads and displays the contents of a binary file.

Click me to see the sample solution


16. Read XML File and Extract Data

Write a PHP program that reads an XML file and extracts specific data from it.

Click me to see the sample solution


17. Read JSON File and Decode Data

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. Calculate MD5 Checksum of a File

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.