📲 Download Utility Tools Apps

EMI Calculator

Download EMI Calculator

SOA Games

Download SOA Games

SOA Technology App

Download SOA Technology

BMI Checker

Download BMI Checker

Task Jira

Download Task Jira

Laughing Adda

Download Laughing Adda

📅 हिंदी कैलेंडर ऐप डाउनलोड करें

Download Shubhcalendar App

Real Interview Questions on PHP File Handling

🎯 PHP File Handling – Interview Questions


🟢 Basic Level

1. What is file handling in PHP?

👉 Managing files (create, read, write, delete) on the server.


2. Difference between fread() and fgets()?

👉 fread() → reads full/partial file
👉 fgets() → reads one line at a time


3. What is the difference between "w" and "a" mode?

👉 "w" → overwrite file
👉 "a" → append data


4. What does feof() do?

👉 Checks if end of file is reached


5. What is $_FILES in PHP?

👉 Superglobal used for file uploads


🟡 Intermediate Level


6. Explain file upload process in PHP.

👉 Steps:

  1. HTML form (enctype="multipart/form-data")
  2. Access via $_FILES
  3. Move file using move_uploaded_file()

7. How do you validate file uploads?

👉 Check:

  • File type (MIME/extension)
  • File size
  • Rename file
  • Restrict executable files

8. What is file_get_contents() vs readfile()?

👉 file_get_contents() → returns content
👉 readfile() → outputs directly


9. What are file permissions in PHP?

👉 Control read/write/execute (e.g., 644, 755)


10. How do you check if a file exists?

file_exists("file.txt");

🔴 Advanced Level (Important for Interviews)


11. What is file locking? Why is it important?

👉 Prevents multiple scripts from writing at same time
👉 Avoids data corruption

flock($file, LOCK_EX);

12. How do you handle large files efficiently?

👉 Use chunk reading:

while(!feof($file)) {
echo fread($file, 1024);
}

13. What are risks in file upload systems?

👉

  • Malware upload
  • Remote code execution
  • File overwrite
  • Directory traversal

14. Difference between unlink() and rmdir()?

👉 unlink() → delete file
👉 rmdir() → delete empty directory


15. What is directory traversal attack?

👉 Accessing restricted files using paths like:

../../etc/passwd

💻 Practical Coding Questions


16. Write code to read a file line by line.

$file = fopen("test.txt", "r");while(!feof($file)) {
echo fgets($file);
}fclose($file);

17. Write code to append data into a file.

$file = fopen("test.txt", "a");
fwrite($file, "New Data\n");
fclose($file);

18. Create a file upload system with validation.

if(isset($_POST['upload'])) {    $fileName = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
$ext = pathinfo($fileName, PATHINFO_EXTENSION); $allowed = ['jpg','png','pdf']; if(in_array($ext, $allowed)) {
move_uploaded_file($tmp, "uploads/" . time() . "." . $ext);
echo "Uploaded";
} else {
echo "Invalid file";
}
}

19. Write code to list all files in a directory.

$files = scandir("uploads");foreach($files as $file) {
if($file != "." && $file != "..") {
echo $file . "<br>";
}
}

20. Write code to delete a file safely.

$file = "test.txt";if(file_exists($file)) {
unlink($file);
echo "Deleted";
} else {
echo "File not found";
}

🧠 Scenario-Based Questions (Very Important)


21. Your file upload system is getting hacked. What will you do?

👉

  • Restrict file types
  • Rename files
  • Store outside public folder
  • Use MIME validation
  • Disable script execution

22. Two users write to same file simultaneously. Problem?

👉 Data corruption
👉 Solution: file locking (flock)


23. Website becomes slow when reading large files. Fix?

👉 Use chunk reading instead of file_get_contents()


24. User uploads .php file and executes it. How to prevent?

👉

  • Block .php extensions
  • Store outside public folder
  • Use .htaccess restrictions

25. How to securely store user-uploaded files?

👉

  • Rename file
  • Validate MIME type
  • Store outside root
  • Limit size
  • Scan content


AI Spiritual Tools & Interactive Experiences

Explore powerful AI-driven tools for daily guidance, spirituality, fun quizzes, and self-discovery.

Today’s Quote

Get inspiring daily quotes powered by AI to motivate and guide your day.

Explore Now

AI Tarot Card Reader

Reveal insights about your future, love, and career with AI tarot readings.

Read Tarot

Love Match Calculator

Check compatibility and love predictions using AI-based analysis.

Check Match

Fortune Cookie

Open an AI fortune cookie and receive wisdom, luck, and fun messages.

Open Cookie

Quiz Categories

Engage with knowledge-based and fun quizzes across multiple categories.

Start Quiz

Panchang Calendar

View daily Panchang, auspicious timings, tithi, nakshatra, and festivals.

View Panchang

Online Numerology

Discover your destiny number, life path, and numerology predictions.

Calculate Now

Spiritual Feeds

Stay connected with spiritual thoughts, mantras, and divine content.

View Feeds

Quiz Hub

Attempt trending quizzes on GK, spirituality, festivals, and more.

Explore Quizzes