您的位置:首页 > 编程语言 > PHP开发

文件相关的操作函数

2016-09-22 23:20 197 查看
1.explode()函数

含义:把字符串打散为数组

explode(separator,string,limit)

separator:必需,规定在哪里分割字符串

string:必需,要分割的字符串

limit:可选,规定返回的数组元素的数目.

      可能的值:

    大于0---返回包含最多limit个元素的数组

     小于0---返回包含除了最后的limit个元素以外所有元素的数组

    0---返回包含一个元素的数组

例子:

<?php

$str = "Hello world. I love Shanghai!";

print_r (explode(" ",$str));

?>

输出:Array ( [0] => Hello [1] => world. [2] => I [3] => love [4] => Shanghai! )

2.realpath()函数

含义:返回绝对路径

    删除所有符号链接(比如:'/./','/../'以及多余的'/'),并返回绝对路径名

    如果失败,该函数返回FALSE

realpath(path)

path    必需,规定要检查的路径

例子:

<?php

    echo realpath("test.txt");

?>

输出:

c:\Inetpub\testweb\test.txt

3.opendir()

含义:函数打开目录句柄

opendir(path,context)

path:必需,规定要打开的目录路径

context:可选,规定目录句柄环境,

返回子:成功则返回目录句柄资源,失败则返回FALSE

例子:

<?php

$dir = "/images/";

if (is_dir($dir)){

  if ($dh = opendir($dir)){

    while (($file = readdir($dh)) !== false){

      echo "filename:" . $file . "<br>";

    }

    closedir($dh);

  }

}

?>

输出:

filename: cat.gif

filename: dog.gif

filename: horse.gif

4.readdir()

含义:打开一个目录,读取它的内容,然后关闭

readdir(dir_handle)

dir_handle:可选。指定之前由 opendir() 打开的目录句柄资源。

       如果该参数未指定,则使用最后一个由 opendir() 打开的链接。

返回值:若成功则返回条目名称(文件名),失败则返回 FALSE。

例子:

<?php

$dir = "/images/";

if (is_dir($dir)){

  if ($dh = opendir($dir)){

    while (($file = readdir($dh)) !== false){

      echo "filename:" . $file . "<br>";

    }

    closedir($dh);

  }

}

?>

输出:

filename: cat.gif

filename: dog.gif

filename: horse.gif

5.closedir()

含义:关闭目录句柄

close(dir_handle)

dir_handle:可选。指定之前由 opendir() 打开的目录句柄资源。

如果该参数未指定,则使用最后一个由 opendir() 打开的链接。

例子:

<?php

$dir = "/images/";

if (is_dir($dir)){

  if ($dh = opendir($dir)){

    while (($file = readdir($dh)) !== false){

      echo "filename:" . $file . "<br>";

    }

    closedir($dh);

  }

}

?>

输出:

filename: cat.gif

filename: dog.gif

filename: horse.gif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php php函数