PHP Files and Directories - Questions and Answers

PHP files and directories

These one line questions on files and directories in PHP are like multiple choice questions.

What is a file?

Answer: A computer file is a computer resource for recording data discretely in a computer storage device.

What is a directory?

Answer:In computing, a directory is a file system cataloging structure which contains references to other computer files, and possibly other directories.

How to move back to the first entry in a given directory while working with it?

Answer: rewinddir() function is used to move back to the first entry in given directory. It resets the directory stream indicated by dir_handle to the beginning of the directory.

What is the purpose of rewind() function?

Answer: The purpose of rewind() function is to set the file pointer to the beginning of the file.
Syntax:
boolean rewind(file);

How to get the user ID of owner of the specified file?

Answer: stat() function returns an indexed array with the statistics of the file. It includes information about file like size of the file, when it was modified, who owns it etc. The user id of the owner can be accessed at index 4 of the array.
Syntax:
array stat(filename);

What is the purpose of file()?

Answer: The purpose of file() function is to read entire file into an array. Each element of the array corresponds to a line in the file, with the newline character still attached. [
array file(string filename [,int use_include_path [,resource context] ]);

Which function is used to delete a file?

Answer: unlink() function is used to delete a file.
Syntax:
bool unlink(filename, context);

What is the use and syntax of file()?

Answer:
Use: The file() function is used to read entire file into an array. file() function returns an array where each element of the array corresponds to a line in the file, with the newline character still attached.
Syntax:
 array file(filename, int include_path, int resource context)

What is the difference between include() and require()?

Answer: include() and require() both functions are used to load an external file into as PHP script runs. The difference between require() and include() is that require() is used when is the file must be loaded and it is required by the application stops the execution of program while include() is used when to load the file that is not necessarily required by the application and the application should continue even if the file is not found.

What is the purpose of fileatime()?

Answer: fileatime() returns the time the file was last accessed in the form of Unix timestamp.

State the purpose of pathinfo()?

Answer: pathinfo() function returns an array that contains information about path of the file. Syntax:
pathinfo(path, options)
Where options can be
PATHINFI_DIRNAME - returns only dirname PATHINFO_BASENAME - returns only basename PATHINFO_EXTENSION - returns only extension

Which function is used to read a line of data from a file?

Answer: fgets() is used to read a line from the file pointed by the file handle.
Syntax:
string fgets(resource handle[, int length]);
fgets() returns a string upto specified length or upto newline character or EOF, whichever comes first.

What is the use of flock() function?

Answer: flock() function is used to lock and release a file.
Syntax:
flock(filehandle, lock, block);

How to open a file to write content to?

Answer:We can open a file to write content to it as given below
$fp = fopen(filename, w);

How to create directory in PHP?

Answer: mkdir() function is used to make a directory. To make a directory with all permissions granted to all users use following code
$dr = "var/www/http/PHP";
mkdir($dr,0777);
Which function is used to open a file for reading and writing?

Answer:fopen() function is used with file modes r and w for reading and writing respectively.

Which functions is used to get the size of a file in PHP?

Answer: filesize() takes a file name as a string parameter and returns the size of file in bytes.

What is the use of basename() function in PHP?

Answer: basename() function takes the complete file path and returns just the file name. Base name is the means of getting the last whole string after the rightmost slash.
Example:
$path = "/var/www/html/abc.php";
$filename = basename($path);

What is the use of opendir() function?

Answer: opendir() function opens a director handle to be used.
Example:
$dh = opendir("dirname");

What is the use of closedir() function in PHP?

Answer: closedir() function is used to close the directory stream indicated by dir_handle. The dir_handle is the handle with which the directory was opened.
Example:
$dh = opendir("dirname");
closedir($dh);

What is the use of readdir() function in PHP?

Answer: readdir() function is used to read entry/item from the directory handle.
Example:
$item = readdir($dh); // reads a single entry from directory
What is the use of rewinddir() function in PHP?

Answer: rewinddir() function resets the directory stream indicated by dir_handle to the beginning of the directory.

What is the use of chdir() function in PHP?

Answer: chdir() function changes PHP's current working directory to directory specified.

What is the use of rmdir() function in php?

Answer: rmdir() removes the directory specified as parameter name. The directory must be empty and relevant permissions must be provided.

Which function is used to check if given parameter is file in php?

Answer: is_file() function is used to check if given parameter is file as it returns true if it is a regular file.

Which function is used to check if it is a directory in php?

Answer: is_dir() function is used to check if parameter passed is a directory or not.

Which function is used to get the group ID of the owner of specified file in PHP?

Answer: filegroup() returns the group ID of the owner of the specified file.

What is the use of fileowner() function in php?

Answer: fileowner() function returns the user ID of owner of the specified file.

What is the use of file_exists() function in php?

Answer: file_exists() function is used to check if the file exists or not. It returns true if file specified exits or false otherwise.