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 code_snippet_id="324388" snippet_file_name="blog_20140503_5_8122871" name="code" class="php">$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 ''; echo $path["extension"];
六、文件系统权限相关的函数实例
创建文件 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 -- 取得文件的所有者

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
