File handling and directories: include and require; file operations (fopen(), fread(), fwrite(), fclose()); file upload ($_FILES, move_uploaded_file()); file download using PHP headers; directory operations (opendir(), readdir(), mkdir(), rmdir())

PHP files पढ़ती और लिखती है (fopen/fread/fwrite/fclose), include और require से code reuse करती है, $_FILES के through uploads receive करती है, और directories manage करती है: portal persistent storage और file uploads पाता है।

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

जब Server Restart होता है, Data चला जाता है

Event portal की registration list एक PHP array में रहती है, जिसका मतलब है request ख़त्म होते ही यह vanish हो जाती है: हर page load blank शुरू होता है। Real portals REMEMBER करते हैं। Data को survive कराने का सबसे simple तरीका इसे server पर एक FILE में लिखना है।

और registration को अक्सर एक UPLOAD चाहिए होता है: एक student का ID card या photo। PHP दोनों handle करती है: files पढ़ना और लिखना, और uploads receive करना। यह lesson portal को persistent storage और file uploads देता है, plus वे code-reuse tools (include, require) जो एक multi-page site को tidy रखते हैं।

Theory

include बनाम require: Reuse Pair

Real sites shared code (एक header, एक database config) को अलग files में split करती हैं जो हर page में pull होती हैं:

  • include 'header.php'; file को pull करता है; अगर यह MISSING हो, PHP एक WARNING देता है और CONTINUE करता है
  • require 'config.php'; file को pull करता है; अगर यह MISSING हो, PHP एक FATAL error throw करता है और STOP करता है

Rule: उन चीज़ों के लिए require इस्तेमाल कीजिए जिनके बिना page काम नहीं कर सकता (database config), और optional extras (एक sidebar) के लिए include। _once variants (include_once, require_once) accidentally एक ही file को दो बार pull होने से रोकते हैं। require बनाम include को correctly choose करना एक common exam question है।

Practical

एक Registrations File पढ़ना और लिखना

<?php
  require 'config.php';   // fatal if missing: the page needs it

  // WRITE (append a registration line)
  $f = fopen("registrations.txt", "a");   // 'a' = append
  fwrite($f, "Riya, Garba Night\n");
  fclose($f);                              // always close

  // READ the whole file back
  $data = file_get_contents("registrations.txt");
  echo nl2br($data);   // show it with line breaks
?>

Theory

File Uploads, Safely

एक file accept करने के लिए, HTML form को 2 चीज़ें चाहिए: method="POST" और enctype="multipart/form-data"। Uploaded file फिर $_FILES superglobal में आती है:

  • $_FILES['idcard']['name']: original filename
  • $_FILES['idcard']['tmp_name']: एक TEMPORARY location जहाँ PHP ने इसे park किया
  • $_FILES['idcard']['size'], ['type'], ['error']

File एक temp spot में बैठती है और request के end पर DELETE हो जाती है जब तक आप इसे save न करें: move_uploaded_file($_FILES['idcard']['tmp_name'], "uploads/" . $safeName)। और चूँकि एक stranger ने यह file choose की, आपको trust करने से पहले इसका type और size validate करना MUST है: एक unchecked upload एक serious security hole है।

Quiz

config.php उन database settings को hold करता है जिन पर पूरा portal depend करता है। आपको इसे include से pull करना चाहिए या require से, और क्यों?

  1. include: यह warn करता है और continue करता है, जो safer है
  2. require: अगर essential config missing हो, page को broken चलने की बजाय एक fatal error के साथ STOP होना चाहिए
  3. दोनों effect में identical हैं
  4. कोई नहीं: config हर file में paste होना चाहिए
Show the answer

require: अगर essential config missing हो, page को broken चलने की बजाय एक fatal error के साथ STOP होना चाहिए

एक ऐसी चीज़ के लिए जिसके बिना page काम नहीं कर सकता, require इस्तेमाल कीजिए: अगर config.php missing हो, require एक fatal error के साथ execution halt करता है, जो exactly right है: अपनी database settings के बिना चलता एक portal confusing, insecure तरीकों से fail होता, तो cleanly stop होना safer है। include सिर्फ़ WARN करता है और CONTINUE करता है, optional pieces (एक sidebar) के लिए appropriate है पर critical dependencies के लिए wrong है। Option C false है: missing-file behaviour ही पूरा difference है। Option D reuse के purpose को defeat करता है। Rule: essentials के लिए require (absent हो तो stop), optional extras के लिए include (absent हो तो continue)।

Think first

Upload जो Disappear हो गया

एक student का upload form काम करता है, $_FILES दिखाता है file आ गई, पर saved file कहीं नहीं है और बाद के requests इसे नहीं ढूँढ पाते। कौन सा single step skip हुआ, और PHP इस तरह behave क्यों करती है? फिर tap कीजिए।

Show the answer

Student ने कभी move_uploaded_file() call नहीं किया। PHP एक upload को एक TEMPORARY location (tmp_name) में park करता है और request के end पर इसे automatically DELETE करता है; जब तक आप इसे उसी request के अंदर एक permanent folder में move न करें, यह चला जाता है। तो $_FILES ने request के DURING file correctly दिखाई, पर कुछ भी इसे save नहीं कर पाया। Fix: request ख़त्म होने से पहले move_uploaded_file($_FILES['idcard']['tmp_name'], "uploads/$safeName")। (और security के लिए पहले type/size validate कीजिए और एक safe filename generate कीजिए।) temp-then-move design deliberate है: यह आपको disk पर commit करने से पहले upload inspect और validate करने देता है।

Watch out

File-Handling Traps

move_uploaded_file भूलना: temp file vanish हो जाती है; upload खो जाता है।

Unvalidated Uploads: type और size check कीजिए; कभी एक user-supplied filename trust मत कीजिए या एक uploaded file execute मत कीजिए।

Optional Files के लिए require: एक missing optional file को page को kill नहीं करना चाहिए; वहाँ include इस्तेमाल कीजिए।

Files Close न करना: fopen/fwrite के बाद fclose(); leaked handles problems cause करते हैं।

ग़लत fopen Mode: 'w' file को TRUNCATES करता है (इसे erase करता है); append के लिए 'a' इस्तेमाल कीजिए: classic data-loss bug।

Theory

Files हो गईं, अब Structured Data

अब portal data persist करता है और uploads accept करता है। पर plain text files structured records के लिए clumsy हैं; structured data exchange करने का modern format JSON है, जिससे आप BCA405-01 में मिले थे। अगला lesson PHP filters (filter_var) से forms properly design और validate करता है और json_encode और json_decode से JSON पढ़ता/लिखता है, ताकि portal JavaScript front-ends वाली same data language बोल सके।

Summary

Key takeaways

  • include एक file pull करता है और missing हो तो WARNS + continue करता है; require एक FATAL error throw करता है और STOPS: essentials के लिए require इस्तेमाल कीजिए।
  • include_once/require_once double inclusion रोकते हैं।
  • File ops: fopen(mode r/w/a), fread/fwrite, fclose; या file_get_contents/file_put_contents; 'w' truncates करता है, 'a' appends करता है।
  • Uploads को method POST + enctype multipart/form-data चाहिए; file $_FILES में आती है (name, tmp_name, size, error)।
  • एक upload save करने के लिए आपको move_uploaded_file() MUST करना पड़ता है, वरना PHP request end पर temp file delete कर देती है; type/size validate कीजिए।
  • Directory ops: opendir/readdir/closedir, scandir, mkdir, rmdir।
  • Memory hook: must-haves के लिए require, और गायब होने से पहले upload move कीजिए।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Advanced PHP and File Management

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

File handling and directories: include and require; file operations (fopen(), fread(), fwrite(), fclose()); file upload ($_FILES, move_uploaded_file()); file download using PHP headers; directory operations (opendir(), readdir(), mkdir(), rmdir()) · Web Framework and Services (Major-12) · Gri-Learn