Home php教程 php手册 php文件操作的简单例子

php文件操作的简单例子

May 25, 2016 pm 04:57 PM

这里讲的文件操作主要是讲php获取文件的主要信息,判断文件的性质,获取文件名和目录名等哦。下面看三个实例
 代码如下 复制代码

//获取文件的主要信息。
$file = "data.txt";

if(is_dir($file))
{
    echo "文件 $file 是个目录";
    echo "
";
}
else
{
    echo "文件 $file 不是目录";
    echo "
";
}

if(is_file($file))
{
    echo "文件 $file 是一个普通文件";
    echo "
";
}

if(is_readable($file))
{
    echo "文件 $file 是可读的";
    echo "
";
}
else
{
    echo "文件 $file 是不可读的";
    echo "
";
}

if(is_writeable($file))
{
   echo "文件 $file 是可写的";
   echo "
";
}
else
{
   echo "文件 $file 是不可写的";
   echo "
";
}
//判断文件的性质。
$path = "/home/prog/php/sayhello.php";

$file_name = basename($path);
$dir_name = dirname($path);

echo "完整路径:".$path;
echo "


";
echo "
";

echo "其中目录名为:".$dir_name;
echo "
";
echo "其中文件名为:".$file_name;
echo "
";
//获取文件名和目录名。
$file = "data.txt";
$dir = "info/newdata";

if(file_exists($file))
{
    echo "当前目录中,文件".$file."存在";
    echo "
";
}
else
{
     echo "当前目录中,文件".$file."不存在";
     echo "
";
}
echo "
";
echo "


";
echo "
";

if(file_exists($dir))
{
    echo "当前目录下,目录".$dir."存在";
    echo "
";
}
else
{
     echo "当前目录下,目录".$dir."不存在";
     echo "
";
}



文章网址:

随意转载^^但请附上教程地址。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24