Extraordinary File Handling in PHP: Top 11+ Interview Questions and Answers

File Handling in PHP

File handling in PHP is an important part of PHP’s web development and programming. It allows developers to create, read, write, and manipulate files on the server. PHP provides a rich set of functions to handle files, making it easier to manage file operations efficiently. In this post, we will explore the basics of file handling in PHP, along with some practical examples. We will also see File Handling in PHP MCQ questions which are mostly asked in an interview.

Join to Become PHP Web Developer - Future Scope Career Guidance

WhatsApp Group Join Now
Telegram Group Join Now
Follow On Instagram Follow Us

Basic Functions for File Handling in PHP

1. fopen(): Opens a file or URL.

2. fread(): Reads from an open file.

3. fwrite(): Writes to an open file.

4. fclose(): Closes an open file.

5. file_get_contents(): Reads the entire file into a string.

6. file_put_contents(): Writes a string to a file.

Modes of File Handling in PHP

When opening a file with fopen(), you need to specify the mode. Common modes include:

  • r: Read-only mode. Starts at the beginning of the file.
  • w: Write-only mode. Opens and truncates the file; or creates a new file if it doesn’t exist.
  • a: Append mode. Opens and writes to the end of the file or creates a new file if it doesn’t exist.
  • r+: Read/Write mode. Starts at the beginning of the file.
  • w+: Read/Write mode. Opens and truncates the file; or creates a new file if it doesn’t exist.
  • a+: Read/Write mode. Preserves file content by writing to the end of the file.

Multiple Choice Questions on File Handling in PHP

Which function is used to open a file in PHP?

a) open_file()

b) fopen()

c) open()

d) file_open()

Answer: b) fopen()


What does the ‘a’ mode in fopen() do?

a) Reads the file

b) Writes to the file

c) Appends to the file

d) Reads and writes to the file

Answer: c) Appends to the file


How do you read a file line by line using PHP?

a) fgets()

b) fread_line()

c) read_line()

d) file_get_line()

Answer: a) fgets()


How do you handle errors when opening a file?

a) Using try-catch

b) Checking if fopen() returns false

c) Ignoring the error

d) Using a custom error handler

Answer: b) Checking if fopen() returns false

13+ Excellence MCQ on Data Types in PHP


What is the difference between ‘w’ and ‘a’ modes in fopen()?

a) ‘w’ writes to the beginning, ‘a’ appends to the end

b) ‘w’ reads the file, ‘a’ writes the file

c) ‘w’ appends to the end, ‘a’ writes to the beginning

d) ‘w’ and ‘a’ both write to the beginning

Answer: a) ‘w’ writes to the beginning, ‘a’ appends to the end


Which function writes a string to a file, overwriting it if it exists?

a) fwrite()

b) file_put_contents()

c) fput()

d) write_file()

Answer: b) file_put_contents()


How do you read the first 10 bytes of a file in PHP?

a) fread($file, 10)

b) file_read($file, 10)

c) fgets($file, 10)

d) file_get_contents($file, 0, 10)

Answer: a) fread($file, 10)


What function would you use to get the size of a file?

a) filesize()

b) file_size()

c) get_filesize()

d) fileinfo()

Answer: a) filesize()


How do you move the file pointer to the beginning of a file?

a) fseek($file, 0)

b) fseek($file, SEEK_SET)

c) fseek($file, 0, SEEK_SET)

d) rewind($file)

Answer: d) rewind($file)


Which function is used to delete a file in PHP
Which function is used to delete a file in PHP

Which function is used to delete a file in PHP?

a) delete_file()

b) unlink()

c) remove_file()

d) file_delete()

Answer: b) unlink()


How do you lock a file for writing in PHP?

a) flock($file, LOCK_EX)

b) flock($file, LOCK_SH)

c) file_lock($file, LOCK_EX)

d) lock_file($file, LOCK_EX)

Answer: a) flock($file, LOCK_EX)


What does the ‘x’ mode in fopen() do?

a) Creates a new file and opens it for writing

b) Opens the file for writing, creating it if it doesn’t exist

c) Opens the file for reading and writing

d) Creates a new file and opens it for reading

Answer: a) Creates a new file and opens it for writing


How do you check if a file exists before opening it?

a) file_exists()

b) is_file()

c) exists()

d) check_file()

Answer: a) file_exists()


Which function is used to rename a file in PHP?

a) rename_file()

b) rename()

c) move_file()

d) change_file_name()

Answer: b) rename()


How do you set the file pointer to a specific position?

a) fseek()

b) fsetpos()

c) rewind()

d) set_pointer()

Answer: a) fseek()


What function is used to get information about a file path?

a) pathinfo()

b) fileinfo()

c) filepath()

d) filedetails()

Answer: a) pathinfo()

15+ Comprehensive MCQ on Variables in PHP


How can you get the last access time of a file?

a) fileatime()

b) filemtime()

c) filectime()

d) filelastaccess()

Answer: a) fileatime()


What function is used to get the file owner?

a) fileowner()

b) getowner()

c) fileinfo()

d) ownerinfo()

Answer: a) fileowner()


How do you get the last modification time of a file?

a) filemtime()

b) filectime()

c) filelastmod()

d) filelastchange()

Answer: a) filemtime()


What is the output of the following code if “example.txt” contains “Hello, World!”?

a) Hello, World!

b) Hello,

c) World!

d) An error

Answer: b) Hello,


What is the output of the following code if “example.txt” contains “Hello, World!”?

a) Hello, World!

b) World!

c) , World!

d) rld!

Answer: b) World!


How can you read a file line by line until the end of the file?

a) !feof($file)

b) !eof($file)

c) !fgets($file)

d) !end_of_file($file)

Answer: a) !feof($file)


How do you check if a file is writable?

a) is_writable

b) is_writeable

c) can_write

d) file_writeable

Answer: a) is_writable


Conclusion on File Handling in PHP

File handling in PHP is essential for web developers working with file operations on the server. By mastering functions like fopen(), fread(), fwrite(), and others, you can efficiently manage file data. The provided multiple-choice questions will help solidify your understanding and prepare you for practical applications.


Follow us on Social Platforms for Daily Interview Quiz

Follow Quiz Lancer Instagram, WhatsApp, Facebook, LinkedInTelegram Channel to solve daily MCQ on PHP Numbers. We share important interview questions which are asked in the interview. Most of the students get good values from this channel and got selected as a developer.


89 / 100
Scroll to Top