1. Which function writes data directly to a file without opening it manually?
file_put_contents writes data to a file in one call. If the file does not exist, it creates it automatically.
file_put_contents('info.txt','Hello World');Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
PHP · Question Set
File Handling & I/O interview questions for placements and exams.
Questions
15
Included in this set
Subject
PHP
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for PHP.
file_put_contents writes data to a file in one call. If the file does not exist, it creates it automatically.
file_put_contents('info.txt','Hello World');For complete preparation, combine this set with full subject-wise practice for PHP. You can also explore other subjects and sets from the links below.
A file pointer represents the current position in an open file. Each read or write operation moves the pointer forward. You can manually reposition it using functions like fseek and rewind.
fseek($handle,0);
Always check return values from functions like fopen, fread, and fwrite. Use file_exists before accessing, and handle failures gracefully using try-catch with file operations or proper conditional checks.
The fopen function opens a file in the specified mode such as read, write, or append. It returns a file handle used for further operations.
fopen('data.txt','r');Using mode 'a' opens the file for writing at the end. The pointer moves to the end and preserves existing content.
fopen('log.txt','a');The fgets function reads a single line from an open file until it reaches a newline or end of file.
$line = fgets($handle);
The fwrite function is used to write data to an open file. If the file is not writable, it returns false.
fwrite($handle,'Hello');
The fclose function closes an open file handle and frees the system resources associated with it.
fclose($handle);
file_get_contents reads the entire file content into a single string, making it a simple way to load small files.
$data = file_get_contents('info.txt');The file_exists function checks if a given path refers to an existing file or directory and returns true or false.
if(file_exists('data.txt')) { echo 'Found'; }The unlink function deletes a file from the file system. It returns true on success or false on failure.
unlink('old.txt');fread requires a file handle and reads data in chunks, while file_get_contents reads the entire file directly. fread is suitable for large files or streaming, whereas file_get_contents is best for small files.
fread($handle, filesize('data.txt'));PHP stores uploaded files in a temporary directory using the $_FILES array. You can then move them to a permanent location using move_uploaded_file. Always validate the file type and size for security.
move_uploaded_file($_FILES['photo']['tmp_name'],'uploads/photo.jpg');
PHP provides mkdir to create a directory, rmdir to remove one, and opendir, readdir, closedir to loop through files inside a folder. scandir can also list all files quickly.
mkdir('uploads');Always check the MIME type and extension, limit maximum file size, rename uploaded files, and store them outside the web root. Never trust the file name sent by the user to avoid code injection.