|
 |
is_dir (PHP 3, PHP 4, PHP 5) is_dir -- Определает, является ли файл директорией Описаниеbool is_dir ( string filename )
Возвращает TRUE, если файл существует и является директорией. Если
filename - это относительное имя файла,
оно будет проверено относительно текущей рабочей директории.
Замечание: Результаты этой функции
кэшируются. Более подробную информацию смотрите в разделе
clearstatcache().
Пример 1. Пример использования функции is_dir()
<?
var_dump(is_dir('a_file.txt')) . "\n";
var_dump(is_dir('bogus_dir/abc')) . "\n";
var_dump(is_dir('..')); ?>
|
Результат выполнения данного примера: bool(false)
bool(false)
bool(true) |
|
Подсказка: Начиная с
PHP 5.0.0, эта функция также может быть
использована с некоторыми упаковщиками url.
Список упаковщиков, поддерживаемых семейством функций
stat(), смотрите в Прил. M.
См. также описание функций chdir(), dir,
opendir(), is_file() и
is_link().
is_dir
derek at nospam dot pmachine dot com
14-Jul-2006 04:10
regarding dmertens-php-note at zyprexia dot com's statement on 24-Sep-2005 09:53
Actually, is_dir is not affected by UID checks in safe_mode. is_dir will even return true if you lack sufficient privileges to read said directory.
dmertens-php-note at zyprexia dot com
24-Sep-2005 08:53
Remember that the owner of the directory has to be the same of the script user, otherwise this function will always return false when PHP is running in safe_mode..
fanfatal at fanfatal dot pl
02-Jul-2005 09:04
Hi
I wrote an usefull function to check is directory exists in directory's tree ;)
<?php
function dir_exists($dir_name = false, $path = './') {
if(!$dir_name) return false;
if(is_dir($path.$dir_name)) return true;
$tree = glob($path.'*', GLOB_ONLYDIR);
if($tree && count($tree)>0) {
foreach($tree as $dir)
if(dir_exists($dir_name, $dir.'/'))
return true;
}
return false;
}
?>
Greatings ;)
...
06-Jun-2005 11:30
With error reporting set to all, and you attempt to do:
echo var_dump(is_dir('bogus_dir/abc'));
as output you will get:
Warning: stat failed for bogus_dir/abc (errno=2 - No such file or directory) in test.php on line 12
bool(false)
If you want to use this with an error reporting level set to all, you will need to use @ to supress the generated error
akashif at freezone dot co dot uk
03-Jun-2005 04:50
just a simple script to for those who dont have the IIS or Apache Dirlisting available , this will make onr for you
<?php
$DirPath=$_GET['DirPath'];
if($DirPath=="")
{
$DirPath='./';
}
if (($handle=opendir($DirPath)))
{
while ($node = readdir($handle))
{
$nodebase = basename($node);
if ($nodebase!="." && $nodebase!="..")
{
if(is_dir($DirPath.$node))
{
$nPath=$DirPath.$node."/";
echo "-> -> -> <a href='dir.php?DirPath=$nPath'>$node</a><br>";
}
else
{
echo "<a href='$node'>$node</a><br>";
}
}
}
}
?>
renich at woralelandia dot com
21-Apr-2005 11:45
Here goes a PHP5.0 link-to-directory listing function and examples
<?php
function list_dirs($path, $target)
{
$list = scandir($path);
foreach ($list as $number => $filename)
{
if ( $filename !== '.' && $filename !== '..' && is_dir("$path/$filename") )
{
$dir = $filename;
$url = apache_request_headers();
if ($target == '')
{
print ("<a href=\"http://$url[Host]/$path/$dir\">$dir</a> <br>\n");
}
else
{
print ("<a href=\"http://$url[Host]$dir\" target=\"$target\">$dir</a> <br>\n");
}
}
}
}
?>
1.- List current directory's directories with no target property.
<?php
list_dirs('.','')
?>
2.- List "libraries" in "pma2" located at my dir with a "_blank" target set
<?php
list_dirs('pma2/libraries','_blank');
?>
or
<?php
list_dirs('/var/www/html/pma2/libraries','_blank');
?>
3.- List all the directories located at the "/Home" dir with "_me" as target.
<?php
list_dirs('/home', ''_me');
?>
I hope you all like it!
Note:
Obviously, the links will now work if the're not in the apache dir... ("htdocs", "html", "www", whatever)
dusted19 at hotmail dot com
19-Mar-2005 02:38
While working on a php project (my first one =( ). I ran into a bug using is_dir() on my winxp w/ servicepack 2 system. I'm using php 4.3.10.. the problem is whenever i pass is_dir() a pathname that is longer then 256 characters long it will ALWAYS return true.
CODE:
<?php
$mpath = "E:\\armoury\\";
ls($mpath);
function ls ($curpath) {
$dir = dir($curpath);
echo("<b>$curpath</b>");
echo "<blockquote>";
while ($file = $dir->read()) {
echo(" <b>");
echo(strlen($curpath.$file));
echo("</b> ");
if($file != "." && $file != "..") {
if (is_dir($curpath.$file)) {
ls($curpath.$file."\\");
} else {
echo(" $file<br>");
}
}
}
$dir->close();
echo "</blockquote>";
return;
}
?>
This is the output.. the number preceding the file/directory is the number of characters in it's full path name. You can see where it trips up at:
257 E:\armoury\[ hiphop - dub - reggae - soul ]\A Tribe Called Quest - 1990 - People's Instinctive Travels And The Paths Of Rhythm\A Tribe Called Quest - People's Instinctive Travels And The Paths Of Rhythm - 10 - Rhythm (Devoted To The Art Of Moving Butts).mp3
The pathname is greater then 256 characters.
229 A Tribe Called Quest - People's Instinctive Travels And The Paths Of Rhythm - 07 - Bonita Applebum.mp3
227 A Tribe Called Quest - People's Instinctive Travels And The Paths Of Rhythm - 08 - Can I Kick It.mp3
233 A Tribe Called Quest - People's Instinctive Travels And The Paths Of Rhythm - 09 - Youthful Expression.mp3
257 E:\armoury\[ hiphop - dub - reggae - soul ]\A Tribe Called Quest - 1990 - People's Instinctive Travels And The Paths Of Rhythm\A Tribe Called Quest - People's Instinctive Travels And The Paths Of Rhythm - 10 - Rhythm (Devoted To The Art Of Moving Butts).mp3\
259 260 267 E:\armoury\[ hiphop - dub - reggae - soul ]\A Tribe Called Quest - 1990 - People's Instinctive Travels And The Paths Of Rhythm\A Tribe Called Quest - People's Instinctive Travels And The Paths Of Rhythm - 10 - Rhythm (Devoted To The Art Of Moving Butts).mp3\index.php\
269 270 277 E:\armoury\[ hiphop - dub - reggae - soul ]\A Tribe Called Quest - 1990 - People's Instinctive Travels And The Paths Of Rhythm\A Tribe Called Quest - People's Instinctive Travels And The Paths Of Rhythm - 10 - Rhythm (Devoted To The Art Of Moving Butts).mp3\index.php\index.php\
Try it yourself on a large directory structure.
flobee
13-Mar-2005 06:13
note: like the main example already shows. tooks me houres to understand und using chdir() to get a full dir scann (especilly on windows and "mounted" network drives)
never forget the tailing slash to find out if you have a directory or not.
<?php
function parse_dir($dir) {
if ($dh = @opendir($dir)) {
while(($file = readdir($dh)) !== false) {
if( !preg_match('/^\./s', $file) ) {
if(is_dir($dir.$file)) {
$newdir = $dir.$file.'/'; chdir($newdir);
echo "IS DIR: $newdir\n";
echo parse_dir($newdir);
} else {
echo $dir.$file."\n";
}
}
}
chdir('..');
}
}
parse_dir('z:/myfolder/mysubfolder/');
?>
puremango dot co dot uk at gmail dot com
08-Feb-2005 11:55
this function bypasses open_basedir restrictions.
<?
function my_is_dir($dir)
{
$tmp_cmd = `ls -dl $dir`;
$dir_flag = $tmp_cmd[0];
if($dir_flag!="d")
{
$dir_flag = $tmp_cmd[1];
}
return ($dir_flag=="d");
}
?>
example:
<?
....
echo is_dir("/somewhere/i/dont/have/access/to");
?>
output:
Warning: open_basedir restriction in effect
<?
....
echo my_is_dir("/somewhere/i/dont/have/access/to");
?>
output:
true (or false, depending whether it is or not...)
---
visit puremango.co.uk for other such wonders
02-Feb-2005 04:12
Unfortunately, the function posted by p dot marzec at bold-sg dot pl does not work.
The corrected version is:
// returns true if folder is empty or not existing
// false if folde is full
function is_empty_folder($dir) {
if (is_dir($dir)) {
$dl=opendir($dir);
if ($dl) {
while($name = readdir($dl)) {
if (!is_dir("$dir/$name")) { //<--- corrected here
return false;
break;
}
}
closedir($dl);
}
return true;
} else return true;
}
tibard at gmail dot com
01-Feb-2005 12:32
use this function to get all files inside a directory (including subdirectories)
<?php
function scan_Dir($dir) {
$arrfiles = array();
if (is_dir($dir)) {
if ($handle = opendir($dir)) {
chdir($dir);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($file)) {
$arr = scan_Dir($file);
foreach ($arr as $value) {
$arrfiles[] = $dir."/".$value;
}
} else {
$arrfiles[] = $dir."/".$file;
}
}
}
chdir("../");
}
closedir($handle);
}
return $arrfiles;
}
?>
rchavezc at ameritech dot net
12-Dec-2004 08:19
This is the function that I use to test for an empty directory:
<?php
function is_emtpy_dir($dirname){
$result=false; if(is_dir($dirname) ){
$result=true; $handle = opendir($dirname);
while( ( $name = readdir($handle)) !== false){
if ($name!= "." && $name !=".."){
$result=false; break; }
}
closedir($handle);
}
return $result;
}
?>
bhuskins at nospam dot christian-horizons dot org
12-Aug-2004 01:51
'Is_empty_folder' posted by andreas at rueping dot net is a nice function. It did give me some grief, though. I don't beleive that it actually closes the directory when it's done (closedir in the wrong spot). I changed it slightly to be the following:
// returns true if folder is empty or not existing
// false if folde is full
<?
function is_emtpy_folder($folder){
if(is_dir($folder) ){
$handle = opendir($folder);
while( (gettype( $name = readdir($handle)) != "boolean")){
$name_array[] = $name;
}
foreach($name_array as $temp)
$folder_content .= $temp;
closedir($handle);if($folder_content == "...") {
return true;
} else {
return false;
}
}
else
return true; }
?>
sly at noiretblanc dot org
28-May-2004 10:10
This is the "is_dir" function I use to solve the problems :
function Another_is_dir ($file)
{
if ((fileperms("$file") & 0x4000) == 0x4000)
return TRUE;
else
return FALSE;
}
or, more simple :
function Another_is_dir ($file)
{
return ((fileperms("$file") & 0x4000) == 0x4000);
}
I can't remember where it comes from, but it works fine.
p dot marzec at bold-sg dot pl
14-Mar-2004 07:33
Simplest version 'is_empty_folder' posted by andreas at rueping dot net
// returns true if folder is empty or not existing
// false if folde is full
function is_empty_folder($dir) {
if (is_dir($dir)) {
$dl=opendir($dir);
if ($dl) {
while($name = readdir($dl)) {
if (!is_dir($name)) {
return false;
break;
}
}
closedir($dl);
}
return true;
} else return true;
}
andreas at rueping dot net
23-Sep-2003 05:29
function checks if a folder is empty or not. Retruns "true" if it is empty and "false" if it is full with data. Also if the folder doesnt even exist, function returns "true".
--------------------------------------------------
--------------------------------------------------
// returns true if folder is empty or not existing
// false if folde is full
<?
function is_emtpy_folder($folder){
if(is_dir($folder) ){
$handle = opendir($folder);
while( (gettype( $name = readdir($handle)) != "boolean")){
$name_array[] = $name;
}
foreach($name_array as $temp)
$folder_content .= $temp;
if($folder_content == "...")
return true;
else
return false;
closedir($handle);
}
else
return true; }
?>
| |