Advanced PHP File Handling MCQs


1. What happens if fopen("file.txt", "r") is called and file does not exist?

A) File will be created
B) Warning is generated
C) Fatal error occurs
D) Returns empty string

👉 Answer: B) Warning is generated
💡 fopen() returns false + warning


2. Which mode allows reading and writing without truncating the file?

A) w+
B) r+
C) a+
D) x+

👉 Answer: B) r+


3. What is the behavior of "a+" mode?

A) Read only
B) Write only
C) Read + write, pointer at beginning
D) Read + write, pointer at end

👉 Answer: D) Read + write, pointer at end


4. What will filesize() return for an empty file?

A) NULL
B) 0
C) FALSE
D) -1

👉 Answer: B) 0


5. Which function is fastest for reading entire file into a string?

A) fread()
B) fgets()
C) file_get_contents()
D) readfile()

👉 Answer: C) file_get_contents()


6. What does readfile() return?

A) File content
B) Number of bytes read
C) Boolean
D) File object

👉 Answer: B) Number of bytes read
💡 It outputs content directly


7. Which function locks a file during writing?

A) lock_file()
B) file_lock()
C) flock()
D) lock()

👉 Answer: C) flock()


8. What is the correct lock type for exclusive access?

A) LOCK_SH
B) LOCK_EX
C) LOCK_UN
D) LOCK_WR

👉 Answer: B) LOCK_EX


9. What happens if two scripts write to same file without locking?

A) PHP prevents it
B) Data corruption may occur
C) File gets deleted
D) Only one script runs

👉 Answer: B) Data corruption may occur


10. What will fwrite() return on success?

A) TRUE
B) Number of bytes written
C) File pointer
D) Content written

👉 Answer: B) Number of bytes written


11. Which function moves pointer to a specific position?

A) fseek()
B) rewind()
C) ftell()
D) fmove()

👉 Answer: A) fseek()


12. What does ftell() return?

A) File size
B) Current pointer position
C) File type
D) File descriptor

👉 Answer: B) Current pointer position


13. What is the default upload max size in PHP (approx)?

A) 1MB
B) 2MB
C) 5MB
D) 10MB

👉 Answer: B) 2MB
💡 Controlled by upload_max_filesize


14. Which PHP setting limits number of uploaded files?

A) max_upload
B) upload_limit
C) max_file_uploads
D) file_limit

👉 Answer: C) max_file_uploads


15. Which is safest way to store uploaded file name?

A) Use original name
B) Use timestamp/random name
C) Use user input
D) Use file size

👉 Answer: B) Use timestamp/random name


16. What is the risk of not validating file uploads?

A) Slow server
B) SQL error
C) Remote code execution
D) UI issue

👉 Answer: C) Remote code execution


17. Which function deletes a file?

A) remove()
B) delete()
C) unlink()
D) erase()

👉 Answer: C) unlink()


18. What happens if unlink() fails?

A) Fatal error
B) Warning + false return
C) Script stops
D) File renamed

👉 Answer: B) Warning + false return


19. Which function clears file status cache?

A) clearcache()
B) file_clear()
C) clearstatcache()
D) resetcache()

👉 Answer: C) clearstatcache()


20. Which function returns file as array (line by line)?

A) fread()
B) fgets()
C) file()
D) readlines()

👉 Answer: C) file()


🧠 Super Tricky (Interview Killer)


21. What happens if you use "w" mode on existing file?

A) Appends data
B) Deletes content immediately
C) Throws error
D) Locks file

👉 Answer: B) Deletes content immediately


22. Which is safer for large files?

A) file_get_contents()
B) fread() in chunks
C) file()
D) readfile()

👉 Answer: B) fread() in chunks


23. What is returned by fopen() on failure?

A) NULL
B) FALSE
C) 0
D) Empty string

👉 Answer: B) FALSE


24. Which function resets pointer to beginning?

A) fseek(0)
B) rewind()
C) reset()
D) start()

👉 Answer: B) rewind()


25. What is a major issue with file_get_contents() on huge files?

A) Slow execution
B) Memory exhaustion
C) Syntax error
D) Security issue

👉 Answer: B) Memory exhaustion