search
HomeBackend DevelopmentPHP Tutorialphp学习基础-文件系统(一) 文件处理,文件权限

一、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 '
'; 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 -- 取得文件的所有者

























Statement
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
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software