← Back to PHP Course | Chapter 9: File Handling | Lesson 1 of 8

PHP File Handling Introduction

File handling PHP को server पर files खोलने, पढ़ने, और लिखने देता है, notes पढ़ने या add करने के लिए एक notebook खोलने जैसा। यह वह तरीका है जिससे एक program खत्म होने के बाद चीज़ें याद रखता है।
Syntax
php
$handle = fopen("filename.txt", "mode");   // false on failure
fwrite($handle, $data);
fclose($handle);

fopen() से Files खोलना

PHP server की filesystem से files पढ़ और लिख सकता है, एक script को data persist करने, activity log करने, या हर task के लिए database की ज़रूरत के बिना downloadable content generate करने देते हुए।

उदाहरण: Opening Files with fopen()

php
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Check whether `$handle`
if ($handle) {
    // Print "File opened successfully" to the output
    echo "File opened successfully";
    // Call `fclose($handle)`
    fclose($handle);
}
?>

fwrite() से Files लिखना

PHP में file operations core functions के एक छोटे set के आसपास घूमते हैं — fopen(), fread()/fwrite(), और fclose() — जो ज़्यादातर programming languages के file APIs में मौजूद open-use-close pattern को mirror करते हैं।

उदाहरण: Writing Files with fwrite()

php
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Call `fwrite($handle, "Hello, file!")`
fwrite($handle, "Hello, file!");
// Call `fclose($handle)`
fclose($handle);
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>

fclose() से Files बंद करना

हमेशा check करें कि कोई file operation succeed हुआ या नहीं, क्योंकि fopen() default रूप से failure पर false (कोई exception नहीं) return करता है, और success मान लेने वाला code आगे confusing errors produce करेगा।

उदाहरण: Closing Files with fclose()

php
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Check whether `$handle === false`
if ($handle === false) {
    // Print "Failed to open file" to the output
    echo "Failed to open file";
// Otherwise, run this branch
} else {
    // Call `fwrite($handle, "Saved data")`
    fwrite($handle, "Saved data");
    // Call `fclose($handle)`
    fclose($handle);
    // Print "File written and closed" to the output
    echo "File written and closed";
}
?>

File Existence Check करना

File permissions और paths real deployments में बहुत मायने रखते हैं: PHP के process को किसी दिए गए path को पढ़ने या लिखने के लिए OS-level permission चाहिए, और एक misconfigured permission production में सबसे common file-handling bugs में से एक है।

उदाहरण: Checking File Existence

php
<?php
// Call `file_put_contents("data.txt", "sample")`
file_put_contents("data.txt", "sample");
// Check whether `file_exists("data.txt") && is_writable("data.txt")`
if (file_exists("data.txt") && is_writable("data.txt")) {
    // Print "File exists and is writable" to the output
    echo "File exists and is writable";
}
?>

unlink() से Files Delete करना

low-level functions से आगे, PHP file_get_contents() और file_put_contents() जैसे convenience wrappers offer करता है जो simple cases के लिए एक ही call में open/read-or-write/close sequence handle करते हैं।

उदाहरण: Deleting Files with unlink()

php
<?php
// Call `file_put_contents("temp.txt", "temporary data")`
file_put_contents("temp.txt", "temporary data");
// Print `file_get_contents("temp.txt") . "\n"` to the output
echo file_get_contents("temp.txt") . "\n";
// Call `unlink("temp.txt")`
unlink("temp.txt");
// Print `file_exists("temp.txt") ? "Still exists" : "Deleted successfully"` to the output
echo file_exists("temp.txt") ? "Still exists" : "Deleted successfully";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. handle इस्तेमाल करने से पहले fopen के false return करने की जाँच न करना।
  2. fclose भूल जाना, ताकि buffered data शायद न लिखा जाए।
  3. गलत mode इस्तेमाल करना, जैसे w, जो एक मौजूदा file erase कर देता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.