Practice 50 PHP File Handling Programming Questions
Q1. Write a PHP script to create a new file named sample.txt.
Input: None
Expected Output: File sample.txt created successfully.
Q2. Write a PHP script to write the text "Hello, World!" to a file named greeting.txt.
Input: None
Expected Output: Text written successfully to greeting.txt.
Q3. Write a PHP script to read the contents of a file named info.txt and display it on the browser.
Input: Contents of info.txt: This is a test file.
Expected Output: This is a test file.
Q4. Write a PHP script to append the text "Have a nice day!" to an existing file named note.txt.
Input: Initial contents of note.txt: Good morning.
Expected Output: File note.txt updated successfully with content:
Good morning.
Have a nice day!
Q5. Write a PHP script to check if a file named example.txt exists, and display a message accordingly.
Input: File example.txt exists.
Expected Output: File example.txt exists.
Q6. Write a PHP script to delete a file named delete_me.txt.
Input: None
Expected Output: File delete_me.txt deleted successfully.
Q7. Write a PHP script to create a directory named new_folder and create a file named newfile.txt inside it.
Input: None
Expected Output: Directory new_folder and file newfile.txt created successfully.
Q8. Write a PHP script to copy a file named source.txt to destination.txt.
Input: File source.txt contains Copy this content.
Expected Output: File destination.txt created with content:
Copy this content.
Q9. Write a PHP script to move a file named move_me.txt to a directory named archive.
Input: File move_me.txt exists in the current directory.
Expected Output: File moved successfully to archive/move_me.txt.
Q10. Write a PHP script to count the number of words in a file named data.txt.
Input: Contents of data.txt: PHP is a scripting language.
Expected Output: Word count: 5
Q11. Write a PHP script to list all files in a directory named my_folder.
Input: Directory my_folder contains file1.txt, file2.txt, image.png.
Expected Output:
file1.txt
file2.txt
image.png
Q12. Write a PHP script to check if a file named readme.md is readable and writable.
Input: File readme.md is readable and writable.
Expected Output:
File readme.md is readable.
File readme.md is writable.
Q13. Write a PHP script to display the file size of largefile.txt in bytes.
Input: File largefile.txt size: 2048 bytes
Expected Output: File size: 2048 bytes
Q14. Write a PHP script to open a file named data.txt in read mode and display the first 10 characters.
Input: Contents of data.txt: abcdefghij1234567890
Expected Output: abcdefghij
Q15. Write a PHP script to check the last modified time of a file named log.txt.
Input: Last modified time: 2024-12-28 14:00:00
Expected Output: Last modified: 2024-12-28 14:00:00
Q16. Write a PHP script to create a log file named activity.log and append the current date and time to it.
Input: None
Expected Output: File activity.log updated with content: 2024-12-29 12:34:56
Q17. Write a PHP script to read a file named numbers.txt and calculate the sum of all numbers in the file.
Input: Contents of numbers.txt: 5 10 15
Expected Output: Sum: 30
Q18. Write a PHP script to read and display the first line of a file named lines.txt.
Input: Contents of lines.txt:
First line
Second line
Third line
Expected Output: First line
Q19. Write a PHP script to write an array of strings to a file named array.txt.
Input: Array: ['One', 'Two', 'Three']
Expected Output: File array.txt contains:
One
Two
Three
Q20. Write a PHP script to split a file named content.txt into multiple files of 100 characters each.
Input: Contents of content.txt: abcdefghijklmnopqrstuvwxyz1234567890 (repeated 5 times)
Expected Output: Multiple files created with 100 characters each.
Q21. Write a PHP script to display the file type of document.pdf.
Input: File document.pdf exists.
Expected Output: File type: application/pdf
Q22. Write a PHP script to rename a file named oldname.txt to newname.txt.
Input: File oldname.txt exists.
Expected Output: File renamed to newname.txt
Q23. Write a PHP script to read a CSV file named data.csv and display its contents as a table.
Input: Contents of data.csv:
Name,Age,City
John,25,New York
Jane,30,London
Expected Output:
Name Age City
John 25 New York
Jane 30 London
Q24. Write a PHP script to count the number of lines in a file named lines.txt.
Input: Contents of lines.txt:
Line 1
Line 2
Line 3
Expected Output: Number of lines: 3
Q25. Write a PHP script to check if a file named example.txt is executable.
Input: File example.txt is not executable.
Expected Output: File example.txt is not executable.
Q26. Write a PHP script to read a file named settings.ini and parse its key-value pairs.
Input: Contents of settings.ini:
host=localhost
user=admin
password=1234
Expected Output:
host: localhost
user: admin
password: 1234
Q27. Write a PHP script to merge the contents of file1.txt and file2.txt into a new file named merged.txt.
Input:
- file1.txt: Hello
- file2.txt: World
Expected Output: File merged.txt contains:
Hello
World
Q28. Write a PHP script to search for the word "PHP" in a file named programming.txt and display its line number.
Input: Contents of programming.txt:
Python is great.
PHP is awesome.
JavaScript is versatile.
Expected Output: Word "PHP" found on line 2.
Q29. Write a PHP script to calculate the size of all files in a directory named my_files.
Input: Directory my_files contains:
- file1.txt: 500 bytes
- file2.txt: 1500 bytes
Expected Output: Total size: 2000 bytes
Q30. Write a PHP script to create a backup of a file named data.txt with a timestamp in the filename.
Input: None
Expected Output: File data_20241229.txt created successfully.
Q31. Write a PHP script to delete all .tmp files from a directory named temp_files.
Input: Directory contains file1.tmp, file2.tmp, and file3.txt.
Expected Output: All .tmp files deleted successfully.
Q32. Write a PHP script to compare two files, fileA.txt and fileB.txt, and display if their contents are identical.
Input:
- fileA.txt: Hello, World!
- fileB.txt: Hello, World!
Expected Output: Files are identical.
Q33. Write a PHP script to display the first 5 lines of a file named longfile.txt.
Input: Contents of longfile.txt:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Expected Output:
Line 1
Line 2
Line 3
Line 4
Line 5
Q34. Write a PHP script to reverse the contents of a file named reverse.txt.
Input: Contents of reverse.txt: abcd1234
Expected Output: File reverse.txt now contains: 4321dcba
Q35. Write a PHP script to calculate the frequency of each word in a file named words.txt.
Input: Contents of words.txt: PHP is easy. PHP is fun.
Expected Output:
PHP: 2
is: 2
easy: 1
fun: 1
Q36. Write a PHP script to read the last 10 lines of a file named logfile.txt.
Input: Contents of logfile.txt:
Line 1
...
Line 20
Expected Output:
Line 11
Line 12
...
Line 20
Q37. Write a PHP script to display the owner of a file named myfile.txt.
Input: File myfile.txt owner: admin
Expected Output: File owner: admin
Q38. Write a PHP script to read a JSON file named data.json and convert it to an associative array.
Input: Contents of data.json:
json
{
"name": "Alice",
"age": 25
}
Expected Output:
name: Alice
age: 25
Q39. Write a PHP script to lock a file named secure.txt for exclusive access during writing.
Input: File secure.txt exists.
Expected Output: File locked and written successfully.
Q40. Write a PHP script to determine if a file named testfile.txt is a symbolic link.
Input: File testfile.txt is not a symbolic link.
Expected Output: File testfile.txt is not a symbolic link.
Q41. Write a PHP script to split a large text file named bigfile.txt into chunks of 1KB each.
Input: File size: 5KB
Expected Output: 5 files created, each 1KB in size.
Q42. Write a PHP script to fetch the MIME type of a file named image.jpg.
Input: File image.jpg exists.
Expected Output: MIME type: image/jpeg
Q43. Write a PHP script to determine if a directory named my_directory is empty.
Input: Directory my_directory contains no files.
Expected Output: Directory my_directory is empty.
Q44. Write a PHP script to list all hidden files in a directory named hidden_files.
Input: Directory contains .hidden1, .hidden2, and file.txt.
Expected Output:
.hidden1
.hidden2
Q45. Write a PHP script to read a file named binaryfile.dat in binary mode and display the first 8 bytes.
Input: Contents of binaryfile.dat: 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48
Expected Output: ABCD
Q46. Write a PHP script to calculate the MD5 hash of a file named hashme.txt.
Input: File contents: Hello, World!
Expected Output: MD5 hash: fc3ff98e8c6a0d3087d515c0473f8677
Q47. Write a PHP script to compress a file named data.txt into data.zip.
Input: File data.txt exists.
Expected Output: File data.zip created successfully.
Q48. Write a PHP script to extract files from archive.zip to a directory named extracted_files.
Input: archive.zip contains file1.txt and file2.txt.
Expected Output: Files extracted successfully to extracted_files.
Q49. Write a PHP script to create a temporary file and write "Temporary data" to it.
Input: None
Expected Output: Temporary file created and written successfully.
Q50. Write a PHP script to display the basename, dirname, and extension of a file path /var/www/html/index.php.
Input: File path: /var/www/html/index.php
Expected Output:
Basename: index.php
Dirname: /var/www/html
Extension: php
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!