PHP File Handling MCQ Questions and Answers

1. How do you open a file in PHP?

a) open('filename', 'mode')
b) fopen('filename', 'mode')
c) file_open('filename', 'mode')
d) open_file('filename', 'mode')

Answer:

b) fopen('filename', 'mode')

Explanation:

The fopen() function is used to open a file in a specified mode in PHP.

2. Which mode in fopen() function is used to read from a file in PHP?

a) 'r'
b) 'w'
c) 'a'
d) 'x'

Answer:

a) 'r'

Explanation:

The 'r' mode in the fopen() function is used for reading from a file.

3. How do you write to a file in PHP?

a) fwrite()
b) write()
c) fputs()
d) Both a) and c)

Answer:

d) Both a) and c)

Explanation:

Both fwrite() and fputs() functions are used to write to a file in PHP.

4. What does the file_get_contents() function do in PHP?

a) Gets the file size
b) Reads the entire file into a string
c) Checks if the file exists
d) Opens a file for writing

Answer:

b) Reads the entire file into a string

Explanation:

The file_get_contents() function reads the entire file into a string.

5. How do you close an opened file in PHP?

a) fclose()
b) close()
c) file_close()
d) end()

Answer:

a) fclose()

Explanation:

The fclose() function is used to close an opened file in PHP.

6. What is the purpose of the file_put_contents() function in PHP?

a) To get the contents of a file
b) To write a string to a file
c) To close a file
d) To open a file for reading

Answer:

b) To write a string to a file

Explanation:

The file_put_contents() function writes a string to a file, creating the file if it does not exist.

7. Which function checks if a file exists in PHP?

a) file_exists()
b) exists_file()
c) is_file()
d) file_present()

Answer:

a) file_exists()

Explanation:

The file_exists() function checks whether a file or directory exists.

8. How do you read a single line from a file in PHP?

a) fgets()
b) readline()
c) get_line()
d) read_file_line()

Answer:

a) fgets()

Explanation:

The fgets() function is used to read a single line from a file.

9. Which mode in fopen() is used to write to a file, clearing its contents in PHP?

a) 'r+'
b) 'w'
c) 'a'
d) 'x'

Answer:

b) 'w'

Explanation:

The 'w' mode in fopen() is used for writing to a file, and it clears the file's contents if it already exists.

10. What does the feof() function do in PHP?

a) Checks if a file exists
b) Checks for the end of a file
c) Closes a file
d) Writes to a file

Answer:

b) Checks for the end of a file

Explanation:

The feof() function checks if the "end-of-file" (EOF) has been reached.

11. How do you append data to an existing file in PHP?

a) Using fopen() with 'a' mode
b) Using file_put_contents() with FILE_APPEND flag
c) Both a) and b)
d) Using fwrite() only

Answer:

c) Both a) and b)

Explanation:

Data can be appended to an existing file using fopen() with 'a' mode or file_put_contents() with the FILE_APPEND flag.

12. Which function deletes a file in PHP?

a) delete_file()
b) remove()
c) unlink()
d) delete()

Answer:

c) unlink()

Explanation:

The unlink() function is used to delete a file in PHP.

13. What is the purpose of the file() function in PHP?

a) Opens a file
b) Reads a file line by line into an array
c) Writes to a file
d) Deletes a file

Answer:

b) Reads a file line by line into an array

Explanation:

The file() function reads a file and stores each line as an element in an array.

14. How can you check the file size in PHP?

a) filesize()
b) get_file_size()
c) file_size()
d) size_of_file()

Answer:

a) filesize()

Explanation:

The filesize() function is used to get the size of a file in bytes.

15. What does the move_uploaded_file() function do in PHP?

a) Moves a file from one location to another
b) Uploads a file to the server
c) Renames a file
d) Copies a file to a new location

Answer:

a) Moves a file from one location to another

Explanation:

The move_uploaded_file() function is used to move an uploaded file to a new location, commonly used in file uploading processes.


Comments