File and Directory Handling - file open-close, copy, rename, delete, directory operations, file upload-download - One Line Questions
1.
To download a file from the server to the client's browser, what HTTP headers are typically required? —
'Content-Disposition: attachment; filename="filename.ext"'
2.
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'? —
'inline'
3.
Which key within the `$_FILES` array holds the original filename of the uploaded file on the client's machine? —
'name'
4.
Which key in the `$_FILES` array indicates the size of the uploaded file in bytes? —
'size'
5.
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? —
'w'
6.
To append data to a file, which mode should be used with fopen()? —
'a'
7.
Which superglobal array in PHP contains information about uploaded files? —
$_FILES
8.
What is the default permission mode for a directory created with mkdir()? —
0755
9.
What is the typical directory structure used by PHP for handling file uploads? —
A temporary directory managed by PHP.
10.
The scandir() function returns an array containing: —
All files and directories within the specified path, including '.' and '..'.
11.
The `glob()` function is useful for finding pathnames matching a pattern. What does `glob('*.txt')` return? —
An array of all files ending with '.txt' in the current directory.
12.
What does the FILE_APPEND flag do when used with file_put_contents()? —
Appends data to the file instead of overwriting it.
13.
What does the 'b' modifier in file opening modes (e.g., 'rb', 'wb') signify? —
Binary mode
14.
Which function is used to change the current working directory? —
chdir()
15.
Which PHP function is used to copy a file from one location to another? —
copy()
16.
When handling file uploads, what is the recommended practice for the temporary directory? —
Ensure it is not web-accessible.
17.
Which PHP function is used to close an open file pointer? —
fclose()
18.
To check if a file exists before attempting to delete it, which function is commonly used? —
file_exists()
19.
Which PHP function is used to write a string to a file? —
file_put_contents()
20.
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? —
file()
21.
What is the primary difference between file() and file_get_contents()? —
file() returns an array of lines, file_get_contents() returns a string.
22.
Which function is used to get the last modified time of a file? —
filemtime()
23.
Which function is used to get the file size of a file in bytes? —
filesize()
24.
Which PHP function is used to open a file and return a file pointer? —
fopen()
25.
Which function is used to get the current working directory? —
getcwd()
26.
When a file is uploaded via an HTML form using `enctype='multipart/form-data'`, where is the uploaded file initially stored by PHP? —
In a temporary directory.
27.
Which PHP function is used to check if a given path is a directory? —
is_dir()
28.
Which PHP function is used to check if a given path is a regular file? —
is_file()
29.
What is the primary security benefit of using `move_uploaded_file()` instead of `copy()` or `rename()` for uploaded files? —
It checks if the file was indeed uploaded via HTTP POST.
30.
The second parameter of mkdir() allows specifying permissions. What does the 'recursive' flag (e.g., `mkdir('path/to/new/dir', 0777, true);`) do? —
It creates parent directories if they do not exist.
31.
The rmdir() function can only remove empty directories. What happens if you try to remove a non-empty directory? —
It returns FALSE and a warning is issued.
32.
The copy() function returns TRUE on success and FALSE on failure. What happens if the destination file already exists? —
It will be overwritten.
33.
Which PHP function is used to create a directory? —
mkdir()
34.
Which PHP function is used to move a temporarily stored uploaded file to a permanent location? —
move_uploaded_file()
35.
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: —
copy() and unlink()
36.
The `$_FILES['userfile']['type']` key provides the MIME type of the uploaded file. However, this value is: —
Provided by the browser and should not be fully trusted.
37.
Which PHP function can be used to read the contents of a file and send it directly to the output buffer for download? —
All of the above (with appropriate headers)
38.
Which PHP function is used to rename or move a file? —
rename()
39.
Which PHP function is used to remove a directory? —
rmdir()
40.
To list the files and directories inside a specified directory, which PHP function can be used? —
scandir()
41.
Which PHP function returns an array of all files and directories within a given path, excluding '.' and '..', sorted alphabetically? —
scandir()
42.
Which function is used to get information about a file, such as size, permissions, and modification time? —
stat()
43.
What does the `Content-Type: application/octet-stream` header typically indicate? —
The file is a generic binary file, and the browser should prompt the user to save it.
44.
The `$_FILES['userfile']['error']` key contains an error code. What does a value of `UPLOAD_ERR_OK` (which is 0) signify? —
The file was uploaded successfully.
45.
What is the primary purpose of the 'r' mode when opening a file with fopen()? —
To read data from the file, starting from the beginning.
46.
What is the return value of fclose() upon successful closure of a file? —
TRUE
47.
What does unlink() return on success? —
TRUE
48.
Which PHP function is used to delete a file? —
unlink()
49.
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? —
UPLOAD_ERR_INI_SIZE