📲 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

🔟 File Handling (PHP)

File handling allows PHP to create, read, update, delete, and manage files on the server.


📂 1. Opening Files (fopen())

Syntax:

fopen(filename, mode);

Modes:

ModeDescription
rRead only
wWrite (overwrite)
aAppend
xCreate new file
r+Read + Write

Example:

$file = fopen("test.txt", "r");

📖 2. Reading Files (fread())

Example:

$file = fopen("test.txt", "r");
$content = fread($file, filesize("test.txt"));
echo $content;
fclose($file);

Line by Line:

while(!feof($file)) {
echo fgets($file);
}

✍️ 3. Writing Files (fwrite())

Example:

$file = fopen("test.txt", "w");
fwrite($file, "Hello World!");
fclose($file);

Append Mode:

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

❌ 4. Closing File (fclose())

fclose($file);

👉 Always close files to free memory.


📤 5. File Upload System

HTML Form:

<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="upload">
</form>

PHP Code:

if(isset($_POST['upload'])) {
$fileName = $_FILES['file']['name'];
$tempName = $_FILES['file']['tmp_name'];
$folder = "uploads/" . $fileName; if(move_uploaded_file($tempName, $folder)) {
echo "File uploaded successfully!";
} else {
echo "Upload failed!";
}
}

🔒 File Upload Validation (IMPORTANT)

$allowed = ['jpg','png','pdf'];
$ext = pathinfo($fileName, PATHINFO_EXTENSION);if(in_array($ext, $allowed)) {
// upload
}

👉 Always validate:

  • File type
  • File size
  • Rename file (avoid overwrite)

📁 6. Directory Handling

Create Directory:

mkdir("new_folder");

Delete Directory:

rmdir("new_folder");

List Files:

$files = scandir("uploads");
print_r($files);

🔍 Check File Exists

if(file_exists("test.txt")) {
echo "File exists";
}

🧠 Important Functions Summary

FunctionUse
fopen()Open file
fread()Read file
fwrite()Write file
fclose()Close file
feof()End of file check
fgets()Read line
file_exists()Check file
move_uploaded_file()Upload file
mkdir()Create folder
scandir()List directory

⚠️ Best Practices

  • Always close files
  • Use file permissions carefully
  • Validate uploads (security)
  • Avoid storing sensitive data in plain text

💡 Mini Practice Task

👉 Create a system that:

  1. Uploads a file
  2. Saves file name in log.txt
  3. Displays all uploaded files


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