Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

PHP File Handling

Total questions: 12

Worksheet time: 6mins

Name
Class
Date
1.

1. What does the readfile() function do in PHP?

a)

Writes content to a file

b)

Reads a file and writes it to the output buffer

c)

Creates a new file

d)

Delete a file

2.

1. What is the purpose of the fopen() function in PHP?

a)

To create a new directory

b)

To delete a file

c)

To close an open file

d)

To open a file for reading or writing

3.

2. What does the fwrite() function do in PHP?

a)

Closes a file

b)

Opens a file for reading

c)

Writes data to a file

d)

Reads data from a file

4.

Which PHP function is used to open a file for reading or writing?

a)

file_open()

b)

open_file()

c)

fopen()

d)

file()

5.

In fopen("mystory.txt", "r"), what does the mode "r" mean?

a)

Read and write

b)

Write only

c)

Read only

d)

Append to the file

6.

What is the correct function to read a specific number of bytes from an open file?

a)

readfile()

b)

fread()

c)

fopen()

d)

fwrite()

7.

What happens if you use fopen("newfile.txt", "w") on a non-existing file?

a)

PHP will show an error

b)

The file will be deleted

c)

The file will be created

d)

Nothing will happen

8.

Why is it important to always close files after handling them in PHP?

a)

To free up system resources

b)

To delete the file

c)

To move the file to another location

d)

To encrypt the file

9.

Ali opens a file with mode "r". He tries to write new data into it, but an error occurs. Why?

a)

The file is locked for editing

b)

The file is opened as read-only, not writable

c)

The file does not exist

d)

The file is already closed

10.

Afif uses fopen("log.txt", "w") to open a file and writes a log message.

What will happen if log.txt already has old data inside?

a)

New data will be added below old data

b)

Old data will be kept, new data added at top

c)

Old data will be deleted and replaced with new data

d)

Nothing will happen because the file is in read mode

11.

Kamut accidentally used "w" mode on a project file that contains important information. What is the immediate risk?

a)

The file becomes read-only

b)

The file might get locked

c)

The existing file content will be permanently erased

d)

The file will not open at all

12.

Sarah wants to add her journal entry at the bottom of journal.txt without losing previous entries. Which mode should she use?

a)

"r"

b)

"w"

c)

"a"

d)

"r+"