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)
Which function is used to open a file in PHP?
a) open()
b) fopen()
c) fileopen()
d) file_read()
What mode is used to open a file for reading only in PHP?
a) 'w'
b) 'r'
c) 'a'
d) 'rw'
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()
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
How do you close an opened file in PHP?
a) close()
b) end()
c) fclose()
d) close_file()
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.
Which PHP function reads the entire contents of a file?
a) readfile()
b) fread()
c) file_get_contents()
d) file_read()
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'
Which function is used to get the file size in PHP?
a) filesize()
b) file_size()
c) fsize()
d) get_size()
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
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">
Which PHP superglobal array is used to handle file uploads?
a) $_FILES
b) $_POST
c) $_GET
d) $_UPLOAD
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()
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.
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()
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.
Which PHP function can be used to check the MIME type of an uploaded file?
a) mime_type()
b) getimagesize()
c) fileinfo()
d) filetype()
What is the default maximum file upload size in PHP?
a) 2MB
b) 5MB
c) 8MB
d) 10MB
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.
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
Which function is used to set file permissions in PHP?
a) chmod()
b) set_permissions()
c) file_chmod()
d) set_file_permissions()
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
How do you check if a file is readable in PHP?
a) is_readable()
b) file_read()
c) can_read()
d) check_readable()
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()
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.
How can you modify the permissions of a file in PHP?
a) chmod()
b) fchmod()
c) set_permission()
d) file_perm()
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.
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.
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.
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
Qno
Answer
1
b) fopen()
2
b) 'r'
3
b) fwrite()
4
d) fopen() with 'a' mode
5
c) fclose()
6
b) It will return false.
7
c) file_get_contents()
8
b) 'w'
9
a) filesize()
10
b) is_writable()
11
b) <input type="file">
12
a) $_FILES
13
a) move_uploaded_file()
14
b) Whether the upload was successful.
15
a) is_uploaded_file()
16
c) Both file size and type restrictions.
17
c) fileinfo()
18
a) 2MB
19
b) The upload fails, and an error is returned.
20
a) By using the accept attribute in the <input> tag.
21
a) chmod()
22
a) 666
23
a) is_readable()
24
a) is_executable()
25
b) Read, write, and execute permission for owner, read and execute for others.