MCQs on Working with Files | PHP Intermediate

Master PHP file operations with this collection of 30 multiple-choice questions. Covering file handling, uploads, permissions, and directory operations, these questions will boost your knowledge of working with files in PHP.


Working with Files

File Handling (open, read, write, append)

  1. Which function is used to open a file in PHP?
    • a) open()
    • b) fopen()
    • c) fileopen()
    • d) file_read()
  2. What mode is used to open a file for reading only in PHP?
    • a) 'w'
    • b) 'r'
    • c) 'a'
    • d) 'rw'
  3. Which of the following functions is used to write data to a file in PHP?
    • a) write()
    • b) fwrite()
    • c) file_put_contents()
    • d) write_to_file()
  4. Which of the following functions is used to append data to a file in PHP?
    • a) fwrite()
    • b) file_put_contents()
    • c) fappend()
    • d) fopen() with 'a' mode
  5. How do you close an opened file in PHP?
    • a) close()
    • b) end()
    • c) fclose()
    • d) close_file()
  6. What will happen if you try to open a non-existent file in 'r' mode using fopen() in PHP?
    • a) It will create the file.
    • b) It will return false.
    • c) It will trigger an error.
    • d) It will return an empty string.
  7. Which PHP function reads the entire contents of a file?
    • a) readfile()
    • b) fread()
    • c) file_get_contents()
    • d) file_read()
  8. Which mode in fopen() is used to open a file for writing, truncating it to zero length?
    • a) 'a'
    • b) 'w'
    • c) 'r+'
    • d) 'rw'
  9. Which function is used to get the file size in PHP?
    • a) filesize()
    • b) file_size()
    • c) fsize()
    • d) get_size()
  10. Which of the following functions will check if a file is writable?
    • a) file_exists()
    • b) is_writable()
    • c) file_can_write()
    • d) file_write()

File Uploads

  1. What is the correct HTML element used for file uploads in a form?
    • a) <input type="text">
    • b) <input type="file">
    • c) <input type="upload">
    • d) <fileinput type="file">
  2. Which PHP superglobal array is used to handle file uploads?
    • a) $_FILES
    • b) $_POST
    • c) $_GET
    • d) $_UPLOAD
  3. Which function is used to move an uploaded file to a different location in PHP?
    • a) move_uploaded_file()
    • b) upload_file()
    • c) file_move()
    • d) rename_file()
  4. What does the $_FILES['file']['error'] value indicate in PHP?
    • a) The file size.
    • b) Whether the upload was successful.
    • c) The file type.
    • d) The file name.
  5. Which of the following PHP functions checks if a file has been uploaded successfully?
    • a) is_uploaded_file()
    • b) file_uploaded()
    • c) is_uploaded()
    • d) uploaded_file()
  6. Which of the following is a valid restriction for uploaded files in PHP?
    • a) File size limit.
    • b) File type restrictions.
    • c) Both file size and type restrictions.
    • d) Only file type restriction.
  7. Which PHP function can be used to check the MIME type of an uploaded file?
    • a) mime_type()
    • b) getimagesize()
    • c) fileinfo()
    • d) filetype()
  8. What is the default maximum file upload size in PHP?
    • a) 2MB
    • b) 5MB
    • c) 8MB
    • d) 10MB
  9. What happens if the file size exceeds the limit set in PHP’s upload_max_filesize directive?
    • a) The file is rejected silently.
    • b) The upload fails, and an error is returned.
    • c) The file is automatically compressed.
    • d) The file is truncated to the allowed size.
  10. How do you specify the allowed file types for upload in an HTML form?
    • a) By using the accept attribute in the <input> tag.
    • b) By setting accept-types in the form tag.
    • c) By using the file_types attribute.
    • d) By specifying it in the PHP script.

File Permissions

  1. Which function is used to set file permissions in PHP?
    • a) chmod()
    • b) set_permissions()
    • c) file_chmod()
    • d) set_file_permissions()
  2. What is the default file permission when a file is created in PHP on a Unix system?
    • a) 666
    • b) 777
    • c) 755
    • d) 644
  3. How do you check if a file is readable in PHP?
    • a) is_readable()
    • b) file_read()
    • c) can_read()
    • d) check_readable()
  4. What function can be used to check if a file is executable in PHP?
    • a) is_executable()
    • b) can_execute()
    • c) file_execute()
    • d) check_executable()
  5. What does the file permission 755 mean on a Unix-based system?
    • a) Read and write permission for owner, read-only for others.
    • b) Read, write, and execute permission for owner, read and execute for others.
    • c) Read-only permission for owner, others can read and write.
    • d) No permissions for anyone.
  6. How can you modify the permissions of a file in PHP?
    • a) chmod()
    • b) fchmod()
    • c) set_permission()
    • d) file_perm()
  7. What does the function umask() in PHP do?
    • a) It sets the file permission mode.
    • b) It retrieves the current file permissions.
    • c) It sets the mask for newly created files.
    • d) It modifies the access time of a file.
  8. Which file permission is required to write to a file in PHP?
    • a) Read permission.
    • b) Write permission.
    • c) Execute permission.
    • d) All of the above.
  9. What permission is required to delete a file in PHP on a Unix system?
    • a) Write permission.
    • b) Execute permission.
    • c) Read permission.
    • d) All of the above.
  10. What does the file permission 644 mean?
    • a) Read-write-execute for owner, read-only for others.
    • b) Read-write-execute for owner, read-only for others.
    • c) Read-write for owner, read-only for others.
    • d) Read-only for owner and others.

Answer Key

QnoAnswer
1b) fopen()
2b) 'r'
3b) fwrite()
4d) fopen() with 'a' mode
5c) fclose()
6b) It will return false.
7c) file_get_contents()
8b) 'w'
9a) filesize()
10b) is_writable()
11b) <input type="file">
12a) $_FILES
13a) move_uploaded_file()
14b) Whether the upload was successful.
15a) is_uploaded_file()
16c) Both file size and type restrictions.
17c) fileinfo()
18a) 2MB
19b) The upload fails, and an error is returned.
20a) By using the accept attribute in the <input> tag.
21a) chmod()
22a) 666
23a) is_readable()
24a) is_executable()
25b) Read, write, and execute permission for owner, read and execute for others.
26a) chmod()
27c) It sets the mask for newly created files.
28b) Write permission.
29a) Write permission.
30c) Read-write for owner, read-only for others.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top