PDA

View Full Version : آموزش: مدیریت فولدر ها (حذف-ایجاد) با php



djsaeedkhan
دوشنبه 26 دی 1390, 00:04 صبح
با سلام خدمت دوستان
اول از همه بگم این کد ها مال من نیست و من کد هایی که در پروزه هام ازشون استفاده می کنم رو میذارم اینجا تا همه بتونن استفاده کنن

ما چند تا دستور داریم که با اون ها می تونیم روی فولد ها مدیریت داشته باشیم
از جمله:

opendir()
readdir()
closedir()


برای اینکه محتویات یه فولدر رو بخونیم و نمایش بدیم از این کد ها استفاده می کنیم. البته شما اگر بصورت تابع بنویسید می تونید کاریش کنید که با ارسال نام فولدر محتویات اون رو نمایش بده و بصورت لینک باشن و دوباره وقتی رو نام زیر فولدر کلیک شد زیر مجموعش رو هم نشون بده.


<?php
// open the current directory
$dhandle = opendir('.');
// define an array to hold the files
$files = array();

if ($dhandle) {
// loop through all of the files
while (false !== ($fname = readdir($dhandle))) {
// if the file is not this file, and does not start with a '.' or '..',
// then store it for later display
if (($fname != '.') && ($fname != '..') &&
($fname != basename($_SERVER['PHP_SELF']))) {
// store the filename
$files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
}
}
// close the directory
closedir($dhandle);
}

echo "<select name=\"file\">\n";
// Now loop through the files, echoing out a new select option for each one
foreach( $files as $fname )
{
echo "<option>{$fname}</option>\n";
}
echo "</select>\n";
?>

با کد زیر می تونید محتویات یه فولدر رو بصورت لیست باکس نمایش بدید.

<?php
echo "<select name=\"file\">\n";
foreach (new DirectoryIterator('.') as $file) {
// if the file is not this file, and does not start with a '.' or '..',
// then store it for later display
if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) {
echo "<option>";
// if the element is a directory add to the file name "(Dir)"
echo ($file->isDir()) ? "(Dir) ".$file->getFilename() : $file->getFilename();
echo "</option>\n";
}
}
echo "</select>\n";
?>

djsaeedkhan
دوشنبه 26 دی 1390, 00:07 صبح
برای حذف یک فولدر می تونید از کد زیر استفاده کنید.

<?php
rmdir( "temporary" );
?>


و باز اگر خواستید حذف فولدر بصورت تابعی باشه می تونید از تابع زیر هم استفاده کنید.

<?php
function deleteDir($dir) {
// open the directory
$dhandle = opendir($dir);

if ($dhandle) {
// loop through it
while (false !== ($fname = readdir($dhandle))) {
// if the element is a directory, and
// does not start with a '.' or '..'
// we call deleteDir function recursively
// passing this element as a parameter
if (is_dir( "{$dir}/{$fname}" )) {
if (($fname != '.') && ($fname != '..')) {
echo "<u>Deleting Files in the Directory</u>: {$dir}/{$fname} <br />";
deleteDir("$dir/$fname");
}
// the element is a file, so we delete it
} else {
echo "Deleting File: {$dir}/{$fname} <br />";
unlink("{$dir}/{$fname}");
}
}
closedir($dhandle);
}
// now directory is empty, so we can use
// the rmdir() function to delete it
echo "<u>Deleting Directory</u>: {$dir} <br />";
rmdir($dir);
}

// call deleteDir function and pass to it
// as a parameter a directory name
deleteDir("temporary");
?>

البته اینو هم بگم اگر بخواید به فولدر که یکسری فایل داخلش هست رو حذف کنید قضیه یکم متفاوت میشه. میشه از کد زیر استفاده کرد. فقط حواستون باشه که ورژن 5.1 رو ساپورت می نمایند

<?php
function deleteDir($dir) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file)
{
if ($file->isDir()) {
rmdir($file->getPathname());
} else {
unlink($file->getPathname());
}
}
rmdir($dir);
}

deleteDir("temporary");
?>

djsaeedkhan
دوشنبه 26 دی 1390, 00:09 صبح
خداییش که ساختن یه فولدر از همه راحت تره

<?php
// if /path/to/my exists, this should return true
// if PHP has the right permissions
mkdir("/path/to/directory", 0777);
?>

البته اگر خواستید یه سری به گوشش بکشید و یه chmod رو هم بهش بدید از این کد استفاده بن مایید (از قصد نوشتم بن مایید)

<?php
// will create /path/to/directory and
// also create /path and /path/to if needed and allowed
mkdir("/path/to/directory", 0777, true);
?>

maxnet12
یک شنبه 10 اسفند 1393, 09:40 صبح
می خواستم چزوری داخل php میشه لیست فایل ها و فولدر ها رو نمایش بده بطوری که وارد فولدر ها بشویم مثل یک explorer

MMSHFE
یک شنبه 10 اسفند 1393, 10:30 صبح
تاپیک سه سال قبل رو بالا نیارین و تاپیک جداگانه ایجاد کنید. این تاپیک قفل شد.