php学习基础-文件系统(一) 文件处理,文件权限
一、PHP系统文件处理
/* PHP文件系统处理 * 所有文件处理都是使用系统函数完成的。 * 是基于Linux/Unix系统为模型 * * 文件系统处理的作用: * 1. 所有的项目离不开文件处理 * 2. 可以用文件长时间保存数据 * 3. 建立缓存, 服务器中文件操作 * * 文件处理 * 1. 文件类型 * 以Linux为模型的, 在Windows只能获取file, dir或unknow 三种类型 * 在Linux/Unix下, block, char, dir, fifo, file, link, unknown和种型 * block :块设置文件,磁盘分区,软驱, cd-rom等 * char: 字符设备,I/O 以字符为单位, 键盘,打印机等 * dir: 目录也是文件的一种 * fifo: * file: * link: * unknown * * filetype("目录或文件名") * * is_array(); * is_int(); * is_string(); * is_null; * is_bool(); * is_dir -- 判断给定文件名是否是一个目录 is_executable -- 判断给定文件名是否可执行 is_file -- 判断给定文件名是否为一个正常的文件 is_link -- 判断给定文件名是否为一个符号连接 is_readable -- 判断给定文件名是否可读 is_uploaded_file -- 判断文件是否是通过 HTTP POST 上传的 is_writable -- 判断给定的文件名是否可写 is_writeable -- is_writable() 的别名 * * * 2. 文件的属性 * file_exists(); * filesize(); * is_readable(); * is_writeable(); * filectime(); * filemtime(); * fileactime(); * stat(); * * 3. 和文件路径相关的函数 * * 相对路径:相对于当前目录的上级和下级目录 * . 当前目录 * .. 上一级目录 * * ./php/apache/index.php * php/apahce/index.php * login.php * ./login.php * ../images/tpl/logo.gif * * * 路径分隔符号 * linux/Unix "/" * windows "\" * * DIRECTORY_SEPARATOR 为不同平台,在Windows \ Linux / * * 不管是什么操作系统PHP的目录分割符号都支技 / (Linux) * * 在PHP和Apache配置文件中如果需要指定目录,也使用/作为目录符号 * * 绝对路径: * / 根路径 * * /images/index.php * * 指的操作系统的根 * 指的是存放网站的文档根目录 * * 分情况 * * 如果是在服务器中执行(通过PHP文件处理函数执行)路径 则 “根”指的就是操作系统的根 * 如果程序是下载的客户端,再访问服务器中的文件时,只有通过Apache访问,“根”也就指的是文档根目录 * * http://www.xsphp.com/logo.gif * * * basename(url) * dirname(url) * pathinfo(url) * * * * * 4. 文件的操作(创建文件,删除文件,移动文件) * 5. 文件的打开与关闭(读文件中的内容, 向文件中写内容) * 6. 文件内部移动指针 * 7. 文件的锁定一些机制处理 * * * 目录的处理 * 1. 目录的遍历 * 2. 目录的创建 * 3. 目录的删除 * 4. 目录的复制 * 5. 统计目录大小 * * * 文件上传和下载 * 1. 上传 * 2. 下载 * * */
二、PHP文件属性函数实例
date_default_timezone_set("PRC"); function getFilePro($fileName){ if(!file_exists($fileName)){ echo "文件或目录{$fileName} 不存在<br>"; return; }else{ echo "文件的类型".filetype($fileName)."<br>"; } if(is_file($fileName)){ echo "这是一个文件<br>"; echo "文件的大小为".getFileSize(filesize($fileName))."<br>"; } if(is_dir($fileName)){ echo "这是一个目录<br>"; } if(is_readable($fileName)){ echo "这个文件可以读<br>"; } if(is_writable($fileName)){ echo "这个文件可以写<br>"; } if(is_executable($fileName)){ echo "这个文件可以执行<br>"; } echo "文件的创建时间:".date("Y-m-d H:i:s",filectime($fileName))."<br>"; echo "文件的修改时间:".date("Y-m-d H:i:s",filemtime($fileName))."<br>"; echo "文件的最后访问时间:".date("Y-m-d H:i:s",fileatime($fileName))."<br>"; } function getFileSize($size){ $dw="Byte"; if($size >= pow(2, 40)){ $size=round($size/pow(2, 40), 2); $dw="TB"; }else if($size >= pow(2, 30)){ $size=round($size/pow(2, 30), 2); $dw="GB"; }else if($size >= pow(2, 20)){ $size=round($size/pow(2, 20), 2); $dw="MB"; }else if($size >= pow(2, 10)){ $size=round($size/pow(2, 10), 2); $dw="KB"; }else { $dw="Bytes"; } return $size.$dw; } getFilePro("demo.txt"); getFilePro("hello");
三、PHP获取文件状态函数
date_default_timezone_set("PRC"); echo '<pre class="brush:php;toolbar:false">'; print_r(stat("demo.txt")); echo '
四、使用文件系统缓存数据方案
$cache=5; //缓存时间$cachefile="cache.txt"; //缓存的文件if(file_exists($cachefile) && (time()-$cache) <p><br> </p> <p>五、文件路径相关函数实例</p> <p></p> <pre name="code" class="sycode">$url1="./aaa/bbb/index.php"; echo basename($url1)."<br>"; //文件名称 echo dirname(dirname($url1))."<br>"; //父级目录 echo dirname($url1)."<br>"; //文件目录echo '<pre class="brush:php;toolbar:false">'; //文件路径信息 print_r($path=pathinfo($url3)); echo '
六、文件系统权限相关的函数实例
创建文件 touch("文件名") 删除文件 unlink("文件路径"); 移动文件 为文件重新命名 rename("当前文件路径", “目录为文件路径”) 复制文件 copy("当前", “目标”); 一定要有PHP执行这个文件权限, Apache, 一个用户 和权限设计有关的函数 ls -l 或 ll _rwxrwxrwx 777 _ 类型 _文件 d 表示是目录 l b rwx 表这个文件的拥有者 r读 w写 x执行 rwx 表这个文件的拥有者所在的组 r读 w写 x执行 rwx 其它用户对这个为文件的权限 r读 w写 x执行 r 4 w 2 x 1 7 7 7 4+2+1 4+2+1 4+2+1 rwx rwx rwx 644 4+2 4 4 rw_ r__ r__ 754 chmod u=rwx,g=rw,o=x chmod 777 demo.php chmod 644 demo.html chown mysql demo.php chgrp apache demo.php chgrp -- 改变文件所属的组 chmod -- 改变文件模式 chown -- 改变文件的所有者 filegroup -- 取得文件的组 fileowner -- 取得文件的所有者

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
