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

PHP Directory Functions

Directory functions PHP को folders के अंदर देखने देते हैं, एक filing cabinet पलटने जैसा। यह अंदर क्या है list कर सकता है, नए folders बना सकता है, या उन्हें हटा सकता है।
Syntax
php
mkdir($path);
rmdir($path);
$items = scandir($path);
is_dir($path);
file_exists($path);

Directories बनाना

scandir($path) किसी दी गई directory के अंदर हर file और subdirectory name का एक array return करता है, current और parent directory represent करने वाली special '.' और '..' entries सहित।

उदाहरण: Creating Directories

php
<?php
// Call `mkdir("sample_dir")`
mkdir("sample_dir");
// Declare `$items`, set to `scandir("sample_dir")`
$items = scandir("sample_dir");
// Print a human-readable dump of `$items`
print_r($items);
?>

Directories हटाना

is_dir() और file_exists() आपको इससे पढ़ने या इसमें लिखने की कोशिश करने से पहले यह check करने देते हैं कि दिया गया path एक directory है या बिल्कुल exist करता है, किसी न मौजूद path पर operate करने से warnings से बचते हुए।

उदाहरण: Removing Directories

php
<?php
// Call `mkdir("sample_dir")`
mkdir("sample_dir");
// Print a detailed dump (with types) of `is_dir("sample_dir")`
var_dump(is_dir("sample_dir"));
// Print a detailed dump (with types) of `file_exists("sample_dir")`
var_dump(file_exists("sample_dir"));
?>

Directory Contents List करना

mkdir($path) एक नई directory बनाता है, और तीसरे argument (recursive) की तरह true pass करना इसे एक ही call में रास्ते में कोई भी missing parent directories बनाने देता है।

उदाहरण: Listing Directory Contents

php
<?php
// Call `mkdir("parent/child", 0755, true)`
mkdir("parent/child", 0755, true);
// Print `is_dir("parent/child") ? "Created including parent" : "Failed"` to the output
echo is_dir("parent/child") ? "Created including parent" : "Failed";
?>

Working Directories

glob($pattern) '*.php' जैसे किसी wildcard pattern से match करने वाले सभी paths ढूँढता है, जो अक्सर scandir() plus manual filtering से ज़्यादा convenient है जब आपको सिर्फ किसी specific type की files की परवाह हो।

उदाहरण: Working Directories

php
<?php
// Call `mkdir("sample_dir")`
mkdir("sample_dir");
// Call `file_put_contents("sample_dir/a.php", "")`
file_put_contents("sample_dir/a.php", "");
// Call `file_put_contents("sample_dir/b.txt", "")`
file_put_contents("sample_dir/b.txt", "");
// Declare `$phpFiles`, set to `glob("sample_dir/*.php")`
$phpFiles = glob("sample_dir/*.php");
// Print a human-readable dump of `$phpFiles`
print_r($phpFiles);
?>

Directories Check करना

rmdir() एक empty directory हटाता है, लेकिन ऐसी directory पर fail हो जाएगा जिसमें अभी भी files हों — एक non-empty directory tree delete करने के लिए पहले उसके contents को recursively delete करना ज़रूरी है।

उदाहरण: Checking Directories

php
<?php
// Call `mkdir("empty_dir")`
mkdir("empty_dir");
// Call `rmdir("empty_dir")`
rmdir("empty_dir");
// Print `file_exists("empty_dir") ? "Still exists" : "Removed successfully"` to the output
echo file_exists("empty_dir") ? "Still exists" : "Removed 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. एक ऐसी directory पर rmdir इस्तेमाल करना जो empty नहीं है, जो fail हो जाता है।
  2. यह भूल जाना कि scandir result में . और .. शामिल करता है।
  3. mkdir से बनाने से पहले यह check न करना कि directory exist करती है।
🔒

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.