PHP Directory Functions
In this page:
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
Directories हटाना
is_dir() और file_exists() आपको इससे पढ़ने या इसमें लिखने की कोशिश करने से पहले यह check करने देते हैं कि दिया गया path एक directory है या बिल्कुल exist करता है, किसी न मौजूद path पर operate करने से warnings से बचते हुए।
उदाहरण: Removing Directories
<?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"));
?>
Login to try C/C++/Java/PHP code in the editor
Directory Contents List करना
mkdir($path) एक नई directory बनाता है, और तीसरे argument (recursive) की तरह true pass करना इसे एक ही call में रास्ते में कोई भी missing parent directories बनाने देता है।
उदाहरण: Listing Directory Contents
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
Working Directories
glob($pattern) '*.php' जैसे किसी wildcard pattern से match करने वाले सभी paths ढूँढता है, जो अक्सर scandir() plus manual filtering से ज़्यादा convenient है जब आपको सिर्फ किसी specific type की files की परवाह हो।
उदाहरण: Working Directories
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Directories Check करना
rmdir() एक empty directory हटाता है, लेकिन ऐसी directory पर fail हो जाएगा जिसमें अभी भी files हों — एक non-empty directory tree delete करने के लिए पहले उसके contents को recursively delete करना ज़रूरी है।
उदाहरण: Checking Directories
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
- एक ऐसी directory पर
rmdirइस्तेमाल करना जो empty नहीं है, जो fail हो जाता है। - यह भूल जाना कि
scandirresult में.और..शामिल करता है। mkdirसे बनाने से पहले यह check न करना कि directory exist करती है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: