Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP File Handling Aptitude Questions and Answers
PHP File Handling Aptitude: This section contains aptitude questions and answers on PHP File Handling.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP File Handling.
1) There are the following statements that are given below, which of them are correct about file handling in PHP?
- PHP supports file handling to create, read, write, upload, and edit files
- We can perform operations with text files only in PHP
- Both of the above
- None of the above
Correct answer: 1
PHP supports file handling to create, read, write, upload, and edit files
Explanation:
PHP supports file handling to create, read, write, upload, and edit files. We can also perform read and write operations with the text files, spreadsheets, etc.
2) Which of the following function is used to read an existing file in PHP?
- openfile()
- readfile()
- readf()
- read_f()
Correct answer: 2
readfile()
Explanation:
The readfile() function is used to read data from an existing file and write to the output buffer.
3) What will the output of below code, if specified file does not exist?
<?php
echo readfile("myfile.txt");
?>
- Error
- Warning
- Null
- 0
Correct answer: 2
Warning
Explanation:
The above code will generate a warning "No such file or directory".
4) Which of the following functions are used for file handling in PHP?
- fopen()
- fread()
- fgets()
- fgetstr()
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct answer: 3
A, B, and C
Explanation:
There are following functions are used for file handling in PHP:
- fopen()
- fread()
- fwrite()
- fgets()
- fputs()
- readfile()
- etc
5) Which of the following function is used to print a message and exit from current php script?
- close()
- exit_script()
- die()
- die_script()
Correct answer: 3
die()
Explanation:
The die() function is used to print the message on webpage and exit from current PHP script, it is similar to the exit() function, the syntax of the die() is given below:
die("message");
6) Which of the following file modes are used with fopen() function in PHP?
- "r"
- "w"
- "x"
- "a+"
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct answer: 4
A, B, C, and D
Explanation:
There are the following file modes are used with fopen() function:
- "r"
- "w"
- "a"
- "x"
- "r+"
- "w+"
- "a+"
- "x+"
7) Which of the following statement is correct about file mode "x" in fopen() in PHP?
<?php
$arr_state = array(
"MP",
"UP",
"AP"
);
print "$arr_state[0], $arr_state[1], ";
if ($arr_state[3] == '') print "DELHI";
?>
- This file mode is used to create a new file for write-only, if the specified file already exists then fopen() function return FALSE in the 'x' file mode.
- This file mode is used to create a new file for write-only, if a specified file already exists then it erases complete data of the file and writes new data into the file.
- The 'x' file mode is not exist for fopen() function.
- None of the above
Correct answer: 1
This file mode is used to create a new file for write-only, if the specified file already exists then fopen() function return FALSE in the 'x' file mode.
Explanation:
The 'x' file mode is used to create a new file for write-only, if the specified file already exists then fopen() function return FALSE in the 'x' file mode.
8) Which of the following function is used to get the length of the specified file?
- filelen()
- filelength()
- filesize()
- fsize()
Correct answer: 3
filesize()
Explanation:
The filesize() function is used to get the length of the specified file. The syntax of filesize() is given below:
filesize("filename");
This function returns an integer values.
9) What is the correct syntax of fread() function in PHP?
- fread($file_ptr, $num_of_bytes_to_read)
- fread($data_buffer)
- fread($file_ptr, $data_buffer)
- None of the above
Correct answer: 1
fread($file_ptr, $num_of_bytes_to_read)
Explanation:
The correct syntax of fread() is :
fread($file_ptr, $num_of_bytes_to_read);
- $file_ptr : Pointer to the opened file
- $num_of_bytes_to_read : Total number of bytes to be read from the opened file.
The fread() function returns data read from the opened file.
10) Which of the following function is used to read a single line from the file in PHP?
- freadline()
- fgets()
- fgetline()
- fline()
Correct answer: 2
fgets()
Explanation:
The fgets() function is used to read a single line from the file in PHP, the syntax of the fgets() function is given below:
fgets($file_pointer);
It returns line that is read from the file.
11) Which of the following function is used to read a single character from the file in PHP?
- fgetchar()
- gethchar()
- fgetc()
- fgetch()
Correct answer: 3
fgetc()
Explanation:
The fgetc() function is used to read a single character from the file, the syntax of the fgetc() function is given below:
fgetc($file_ptr);
This function returns a single character that is read from the file.
12) Which of the following function is used to delete a specified file?
- fdel()
- fdelete()
- unlink()
- deletefile()
Correct answer: 3
unlink()
Explanation:
The unlink() function is used to delete a specified file. The syntax is given below:
bool unlink("filename");
The unlink() function returns true, if file is deleted successfully, otherwise it returns false.
13) Which of the following function is used to check a specified file is exist of not?
- is_file_exists()
- file_exists()
- file_exist()
- fileexist()
Correct answer: 2
file_exists()
Explanation:
The file_exists() function is used to check a specified file is exists of or not. We can understand this using below example:
<?php
if (file_exists('myfile.txt'))
{
echo 'file exists!';
}
else
{
echo 'file does not exist';
}
?>
14) Which of the following function is used to copy the content of file into another file?
- fcopy()
- filecopy()
- file_copy()
- copy()
Correct answer: 4
copy()
Explanation:
The copy() is used to copy the content of a file to the another specified file. The syntax of copy() function is given below:
bool copy($source, $dest);
15) Which of the following function is used to check the end of the file?
- feof()
- eof()
- end_of_file()
- endoffile()
Correct answer: 1
feof()
Explanation:
The feof() function is used to check the end of file while reading data from the file. We can understand feof() function properly using below code:
<?php
$file_ptr = fopen("my.txt", "r") or die("file opening failed");
while (!feof($file_ptr))
{
echo fgetc($file_ptr);
}
fclose($file_ptr);
?>
16) Which of the following function is used to read the complete file content?
- read_complete_file()
- read_complete_data()
- file_get_contents()
- None of the above
Correct answer: 3
file_get_contents()
Explanation:
The file_get_contents() function is used to read the entire file content from the specified file. The syntax is given below:
file_get_contents("myfile.txt")
17) Which of the following directive is used to enable/disable file uploading in the "phh.ini" file?
- fileupload
- file_upload
- fileuploads
- file_uploads
Correct answer: 4
file_uploads
Explanation:
The file_uploads directive is used to enable/disable file uploading in the "phh.ini" file. If we want to allow file uploading then we need to write like this:
file_uploads = On
18) Which of the following superglobal variable is used to upload a file in PHP?
- $_UPLOAD_FILE
- $_UPLOAD_FILES
- $_FILE
- $_FILES
Correct answer: 4
$_FILES
Explanation:
The $_FILES super global variable is used to upload a file in PHP.
19) Which of the following condition is used to check size limit 1KB to upload a file?
- if ($_FILES["fileToUpload"]["size"] > 1024)
- if ($_FILES["fileToUpload"]["size"] > 1KB)
- if ($_FILES["fileToUpload"]["limit"] > 1024)
- None of the above
Correct answer: 1
if ($_FILES["fileToUpload"]["size"] > 1024)
Explanation:
The $FILES is a super global variable, here we use size then we can check size limit 1KB to upload a file.
20) Which of the following function is used to close a file in PHP?
- close()
- f_close()
- fclose()
- None of the above
Correct answer: 3
fclose()
Explanation:
The fclose() function is used to close a file, the syntax of flcose() function is given below:
fcloe($file_ptr);