File and Directory Handling - file open-close, copy, rename, delete, directory operations, file upload-download - Question Bank

1. When handling file uploads, what is the recommended practice for the temporary directory?
A) Ensure it is not web-accessible.
B) Make it world-writable for maximum compatibility.
C) Store uploaded files there permanently.
D) Use the same directory as the PHP script.
2. Which function is used to get the file size of a file in bytes?
A) filesize()
B) file_size()
C) get_size()
D) size_of_file()
3. Which function is used to get the last modified time of a file?
A) filemtime()
B) last_modified()
C) get_mtime()
D) file_last_modified()
4. The `glob()` function is useful for finding pathnames matching a pattern. What does `glob('*.txt')` return?
A) An array of all files ending with '.txt' in the current directory.
B) An array of all files starting with '.txt'.
C) A single string representing the first '.txt' file found.
D) An error if no '.txt' files are found.
5. Which PHP function returns an array of all files and directories within a given path, excluding '.' and '..', sorted alphabetically?
A) scandir()
B) glob()
C) opendir()
D) dir()
6. Which function is used to change the current working directory?
A) chdir()
B) change_dir()
C) cd()
D) set_cwd()
7. Which function is used to get the current working directory?
A) getcwd()
B) pwd()
C) current_dir()
D) get_cwd()
8. If you want to force the browser to display the file inline (if possible) rather than downloading it, which header should be used instead of 'attachment'?
A) 'inline'
B) 'display'
C) 'view'
D) 'open'
9. What does the `Content-Type: application/octet-stream` header typically indicate?
A) The file is a generic binary file, and the browser should prompt the user to save it.
B) The file is a text file.
C) The file is an image.
D) The file is an HTML document.
10. Which PHP function can be used to read the contents of a file and send it directly to the output buffer for download?
A) readfile()
B) file_get_contents()
C) fopen() followed by fread()
D) All of the above (with appropriate headers)
11. To download a file from the server to the client's browser, what HTTP headers are typically required?
A) 'Content-Description: File Transfer'
B) 'Content-Type: application/octet-stream'
C) 'Content-Disposition: attachment; filename="filename.ext"'
D) 'Content-Length: size'
12. What is the primary security benefit of using `move_uploaded_file()` instead of `copy()` or `rename()` for uploaded files?
A) It checks if the file was indeed uploaded via HTTP POST.
B) It automatically sets file permissions.
C) It encrypts the uploaded file.
D) It prevents overwriting existing files.
13. Which PHP function is used to move a temporarily stored uploaded file to a permanent location?
A) move_uploaded_file()
B) upload_file()
C) save_uploaded_file()
D) move_file()
14. Which constant represents the error code for a file that is too large to be uploaded, as defined by `upload_max_filesize` in php.ini?
A) UPLOAD_ERR_INI_SIZE
B) UPLOAD_ERR_FORM_SIZE
C) UPLOAD_ERR_MAX_SIZE
D) UPLOAD_ERR_FILE_TOO_LARGE
15. The `$_FILES['userfile']['error']` key contains an error code. What does a value of `UPLOAD_ERR_OK` (which is 0) signify?
A) The file was uploaded successfully.
B) There was a general upload error.
C) The file is too large.
D) No file was uploaded.
16. Which key in the `$_FILES` array indicates the size of the uploaded file in bytes?
A) 'size'
B) 'filesize'
C) 'bytes'
D) 'length'
17. The `$_FILES['userfile']['type']` key provides the MIME type of the uploaded file. However, this value is:
A) Provided by the browser and should not be fully trusted.
B) Guaranteed to be accurate by PHP.
C) Determined by the server's operating system.
D) Always 'application/octet-stream'.
18. Which key within the `$_FILES` array holds the original filename of the uploaded file on the client's machine?
A) 'name'
B) 'filename'
C) 'originalname'
D) 'clientname'
19. Which superglobal array in PHP contains information about uploaded files?
A) $_FILES
B) $_UPLOAD
C) $_FILESUP
D) $_FILEINFO
20. When a file is uploaded via an HTML form using `enctype='multipart/form-data'`, where is the uploaded file initially stored by PHP?
A) In a temporary directory.
B) Directly in the web server's document root.
C) In the directory where the PHP script resides.
D) In the user's browser cache.
21. What is the typical directory structure used by PHP for handling file uploads?
A) A temporary directory managed by PHP.
B) The root directory of the web server.
C) The user's home directory.
D) A directory specified in the php.ini file.
22. Which function is used to get information about a file, such as size, permissions, and modification time?
A) stat()
B) file_info()
C) get_file_stats()
D) file_stat()
23. The scandir() function returns an array containing:
A) All files and directories within the specified path, including '.' and '..'.
B) Only files within the specified path.
C) Only directories within the specified path.
D) The total number of files and directories.
24. To list the files and directories inside a specified directory, which PHP function can be used?
A) scandir()
B) list_dir()
C) dir_scan()
D) readdir()
25. Which PHP function is used to check if a given path is a directory?
A) is_dir()
B) is_directory()
C) check_dir()
D) dir_type()
26. Which PHP function is used to check if a given path is a regular file?
A) is_file()
B) is_regular_file()
C) check_file()
D) file_type()
27. What does the FILE_APPEND flag do when used with file_put_contents()?
A) Appends data to the file instead of overwriting it.
B) Appends the filename to the data.
C) Appends a newline character.
D) Appends metadata to the file.
28. Which PHP function is used to write a string to a file?
A) file_put_contents()
B) write_file()
C) fwrite()
D) put_contents()
29. What is the primary difference between file() and file_get_contents()?
A) file() returns an array of lines, file_get_contents() returns a string.
B) file() can only read text files, file_get_contents() can read binary files.
C) file() is faster than file_get_contents().
D) file() requires a file pointer, file_get_contents() does not.
30. Which PHP function is used to read the contents of a file into an array, with each array element representing a line of the file?
A) file()
B) read_file()
C) file_get_contents()
D) read_lines()
31. The rmdir() function can only remove empty directories. What happens if you try to remove a non-empty directory?
A) It returns FALSE and a warning is issued.
B) It removes the directory and its contents.
C) It throws an exception.
D) It returns TRUE but the directory remains.
32. Which PHP function is used to remove a directory?
A) rmdir()
B) delete_dir()
C) remove_directory()
D) dir_rm()
33. The second parameter of mkdir() allows specifying permissions. What does the 'recursive' flag (e.g., `mkdir('path/to/new/dir', 0777, true);`) do?
A) It creates parent directories if they do not exist.
B) It sets recursive read permissions.
C) It makes the directory recursively writable.
D) It ignores existing parent directories.
34. What is the default permission mode for a directory created with mkdir()?
A) 0777
B) 0755
C) 0644
D) 0700
35. Which PHP function is used to create a directory?
A) mkdir()
B) create_dir()
C) new_dir()
D) directory_make()
36. To check if a file exists before attempting to delete it, which function is commonly used?
A) file_exists()
B) is_file()
C) check_file()
D) exists()
37. What does unlink() return on success?
A) TRUE
B) FALSE
C) The file size
D) NULL
38. Which PHP function is used to delete a file?
A) unlink()
B) delete_file()
C) rm()
D) remove_file()
39. If the source and destination paths in rename() are on different filesystems, the behavior depends on the operating system. On POSIX systems, this operation is usually an alias for:
A) move()
B) copy() and unlink()
C) link()
D) symlink()
40. Which PHP function is used to rename or move a file?
A) rename()
B) move_file()
C) change_name()
D) file_rename()
41. The copy() function returns TRUE on success and FALSE on failure. What happens if the destination file already exists?
A) It will be overwritten.
B) An error will be thrown.
C) The copy operation will fail.
D) It will be appended.
42. Which PHP function is used to copy a file from one location to another?
A) copy()
B) file_copy()
C) cp()
D) duplicate_file()
43. What is the return value of fclose() upon successful closure of a file?
A) TRUE
B) FALSE
C) NULL
D) The file pointer resource
44. Which PHP function is used to close an open file pointer?
A) fclose()
B) close_file()
C) file_close()
D) free_pointer()
45. What does the 'b' modifier in file opening modes (e.g., 'rb', 'wb') signify?
A) Binary mode
B) Buffered mode
C) Backup mode
D) Blocking mode
46. To append data to a file, which mode should be used with fopen()?
A) 'w+'
B) 'a+'
C) 'r+'
D) 'a'
47. Which mode in fopen() is used to open a file for writing only; place the file pointer at the beginning of the file and truncate the file length to 0?
A) 'w'
B) 'r'
C) 'a'
D) 'x'
48. What is the primary purpose of the 'r' mode when opening a file with fopen()?
A) To write data to the file, creating it if it doesn't exist.
B) To read data from the file, starting from the beginning.
C) To append data to the file, creating it if it doesn't exist.
D) To create a new file for writing, truncating it if it exists.
49. Which PHP function is used to open a file and return a file pointer?
A) fopen()
B) file_open()
C) open_file()
D) open_resource()