File handling mcq in php

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “File System and PHP”.

1. The filesize() function returns the file size in ___________
a) bits
b) bytes
c) kilobytes
d) gigabytes
View Answer

Answer: b
Explanation: The function filesize() returns the size of the specified file and it returns the file size in bytes on success or FALSE on failure.

2. Which one of the following PHP function is used to determine a file’s last access time?
a) fileltime()
b) filectime()
c) fileatime()
d) filetime()
View Answer

Answer: c
Explanation: The fileatime() function returns a file’s last access time in Unix timestamp format or FALSE on error.

3. Which one of the following function is capable of reading a file into an array?
a) file()
b) arrfile()
c) arr_file()
d) file_arr()
View Answer

Answer: a
Explanation: The function file() will read the entire file into an array.

4. Which one of the following function is capable of reading a file into a string variable?
a) file_contents()
b) file_get_contents()
c) file_content()
d) file_get_content()
View Answer

Answer: b
Explanation: The function file_get_contents() reads a file into a string. This is the preferred way to read the contents of a file into a string as it will use memory mapping techniques.

5. Which one of the following function is capable of reading a specific number of characters from a file?
a) fgets()
b) fget()
c) fileget()
d) filegets()
View Answer

Answer: a
Explanation: The function fgets() will return a line from an open file. This stops returning on a new line, at the specified length, or at EOF, whichever comes first. Its prototype is string fgets(resource handle [, int length]). If the optional length parameter is omitted, 1024 character is assumed.

6. Which one of the following function operates similarly to fgets(), except that it also strips any HTML and PHP tags form the input?
a) fgetsh()
b) fgetsp()
c) fgetsa()
d) fgetss()
View Answer

Answer: d
Explanation: The function fgetss() returns a line, with HTML and PHP tags removed, from an open file. This function operates similarly to fgets(), except that it also strips any HTML and PHP tags form the input.

7. Which one of the following function outputs the contents of a string variable to the specified resource?
a) filewrite()
b) fwrite()
c) filewrites()
d) fwrites()
View Answer

Answer: b
Explanation: The function fwrite() writes to an open file. This will stop at the end of the file or when it reaches the specified length, whichever comes first.

8. Which function sets the file filename last-modified and last-accessed times?
a) sets()
b) set()
c) touch()
d) touched()
View Answer

Answer: c
Explanation: The function touch() will set the access and modification time of the specified file. Syntax is touch(filename, time, atime).

9. Which function is useful when you want to output the executed command result?
a) out_cmm()
b) out_system()
c) cmm()
d) system()
View Answer

Answer: d
Explanation: The function system() in PHP is same as the C version of the function as in that it executes the given command and outputs the result. This function also tries to automatically flush the web server’s output buffer after each line of output if PHP is running as a server module.

10. Which one of the following function reads a directory into an Array?
a) scandir()
b) readdir()
c) scandirectory()
d) readdirectory()
View Answer

Answer: a
Explanation: It returns an array consisting of files and directories found in directory or returns FALSE on error.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

Next Steps:

  • Get Free Certificate of Merit in PHP Programming
  • Participate in PHP Programming Certification Contest
  • Become a Top Ranker in PHP Programming
  • Take PHP Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

File handling mcq in php

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.