Summary of PHP directory operation functions_PHP tutorial

WBOY
Release: 2016-07-13 17:15:19
Original
888 people have browsed it

This article summarizes some functions and usage methods of PHP directory operations, including: creating directories, traversing directories, reading directories, closing directories, opening directories, etc. About PHP directory operation functions, friends in need can refer to them.

php creates directory folder function mkdir(), its structure is as follows:

kdir(string $dirname,[int $mode])

The parameter $dirname is the name of the directory you want to create, and the parameter $mode is optional and is an integer variable indicating the creation mode.

Example:

The code is as follows Copy code
 代码如下 复制代码

  $name="php";
$d=mkdir($name,0777); /* 0777表示最大访问权限 */
if($d){
echo "创建成功";
}
else echo "创建不成功";
?>

$name="php";
$d=mkdir($name,0777); /* 0777 indicates maximum access rights */

if($d){

echo "Created successfully";

}
代码如下 复制代码
function directory($dir){ /* 声明函数 */
$dp=opendir($dir); /* 打开目录 */
while($file=readdir($dp)){ /* 读取目录 */
if($file !="." && $file !=".."){ /* 判断是否有"."或者".."文件 */
$path=$dir. "/". $file; /* 取得目录路径 */
if(is_dir($path)){ /* 判断是否有子目录 */
directory($path); /* 函数递归调用 */
}
else echo $path. "
"; /* 显示文件 */
   }
  }
  closedir($dp);
 }
 
 directory("e:wp");
?>
else echo "Creation failed";

?>


There are many ways to traverse directories in PHP. Here, we will introduce in detail how to traverse files in PHP directories through examples.

Code:

The code is as follows Copy code
function directory($dir){ /* Declare function */

$dp=opendir($dir); /* Open directory */
代码如下 复制代码

$dir=opendir("study");
while($read=readdir($dir)){
print($read."
");
 }
?>

while($file=readdir($dp)){ /* Read directory */

if($file !="." && $file !=".."){ /* Determine whether there is a "." or ".." file */

$path=$dir. "/". $file; /* Get the directory path */

If(is_dir($path)){ /* Determine whether there is a subdirectory */

Directory($path); /* Function recursive call */

}

else echo $path. "
"; /* Display file */

}

}
 代码如下 复制代码
$mulu="study";
$dir=opendir($mulu);
closedir($dir);
?>
closedir($dp); } directory("e:wp"); ?> The PHP directory reading function readdir() can read all files and folders in the directory. Its structure is as follows: readdir($dp); The parameter $dp is the resource object returned by using the function opendir() to open the directory. The function returns the file name in the directory. Example:
The code is as follows Copy code
"); } ?>
php closes the directory function using closedir(), its structure is as follows: closedir($dp) The parameter $dp is the resource object returned by using the function opendir() to open the directory. Example:
The code is as follows Copy code
$mulu="study";<🎜> $dir=opendir($mulu);<🎜> closedir($dir);<🎜> ?>

Using the function closedir() to successfully close a directory does not return the value 1, so you cannot use the if statement to determine whether the directory is closed successfully

I have introduced a series of file operations in PHP before, and then the author will introduce how to operate the directory. The function of the PHP directory function is similar to that of the file function. Here we first introduce the open directory function opendir(). Its structure is as follows:


opendir(string $path)


The parameter $path is the path to the directory to be opened, and the function will return a handle to the opened directory, which is used to store the current directory resources. Before opening a directory, you must first determine whether the directory exists, using the is_dir() function.

Example:

?>
The code is as follows
 代码如下 复制代码

if (is_dir("stufdy")){
opendir("studfy");
print_r("目录成功打开");
}
else
echo "目录不存在";
?>

Copy code

if (is_dir("stufdy")){
opendir("studfy");

print_r("Directory opened successfully");

}

else
 代码如下 复制代码


   $f=fopen("php.txt","r");
 echo fgets($f)."
";  /*输出第一行*/
 echo fgets($f)."
";  /*输出第二行*/
 rewind($f);             /*指针返回文件头*/
 echo fgets($f);         /*输出第一行*/   
?>

echo "Directory does not exist";

The PHP pointer function rewind() can set the file location pointer to the beginning of the file. Its structure is as follows: bool rewind (resource $handle ); The function returns a Boolean value, true if successful and false if failed. Example:
The code is as follows Copy code
"; /*Output the first line*/ echo fgets($f)."
"; /*Output the second line*/
rewind($f); /*The pointer returns the file header*/
echo fgets($f); /*Output the first line*/ ?> http://www.bkjia.com/PHPjc/628848.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628848.htmlTechArticleThis article summarizes some functions and usage methods of PHP directory operations, including: creating directories, traversing directories, reading Fetch directory, close directory, open directory, etc. About php directory operations...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!