search
HomeBackend DevelopmentPHP Tutorial php资料操作相关函数

php文件操作相关函数

一、文件类型
1.filetype()
获取文件类型函数,参数为字符串(路径+文件名)
返回值为字符串,file(普通文件),dir(目录)或unknown(未知文件)
2.is_file() is_dir()
判断是否是该类型,返回布尔值
二、文件属性(以下函数参数均为文件名)
1.file_exists() 检查文件或目录是否存在
2.filesize() 取得文件大小,出错返回false
3.is_readable() 是否可读
4.is_writable() 是否可写
5.is_executable() 是否可执行
6.filectime() 获取创建时间
7.filemtime() 获取修改时间
8.fileatime() 获取访问时间
9.stat() 获取文件大部分属性值
10.clearstatcache() 清除被PHP缓存的该文件信息
三、解析目录
1.basename(path,[suffix]) 返回路径中的文件名部分,第二个参数为扩展名(如"php"或".php"),如给出则返回值中不再有扩展名
2.dirname(path) 返回去掉文件名后的目录名
3.pathinfo() 返回一个关联数组,包括dirname(目录名),basename(基本名),extension(扩展名)
四、遍历目录
1.opendir() 打开指定目录,返回可供其他目录函数使用的目录句柄。失败返回false
2.readdir() 读取指定目录,参数为目录句柄,返回当前目录指针位置的一个文件名,并将指针后移一们。没有更多文件返回false
3.closedir() 关闭指定目录,参数为目录句柄
4.rewinddir() 倒回目录句柄,参数为目录句柄,将目录指针重置到开始处
五、建立和删除目录
1.mkdir() 建立新目录,参数为目录名
2.rmdir() 删除目录,被删除的只能是空目录,如非空,则必须先进入目录,将其中的文件用unlink()函数删除
六、复制或移动目录
1.复制:php中无特定函数,必须先新建目录mkdir(),再使用copy()函数复制每个文件。
2.移动:先复制,后删除原目录
七、文件打开与关闭
1.fopen(filename,mode[,use_include_path[,zcontext]]) 打开文件,参数为文件名,文件模式,第三个参数可选,设为1会使PHP考虑配置指令include_path中指定的路径,第四个参数可选,设置允许文件名称以协议名称开始,如http://。返回文件指针,失败返回false。
模式总结:
r 只读
r+ 读写
w 只写(文件存在,则删除原有数据,文件不存在,则创建这个文件)
w+ 读写(同w)
x 写入(文件存在,返回false,文件不存在则创建,仅本地)
x+ 读写(同x)
a 写入(指针指向文件尾,文件不存在则创建)
a+ 写入(同a)
b 二进制模式
t 文本模式
2.fclose() 关闭
八、操作文件内容
1.fwrite(handle,string[,length]) 写入字符串。nr为行结束字符。返回写入的字符数,失败返回false.
2.fread(handle,length) 读取打开的文件
3.file_get_contents() 将文件读入字符串
4.fgets(handle[,length]) 返回一行
5.fgetc() 返回字符
6.file() 把文件读入一个数组,每行为一个元素。
7.readfile() 读取一个文件,输出到输出缓冲
8.feof() 判断是否到达文件结束处,是则返回true
9.file_get_contents()
10.访问远程文件:配置文件中激活"allow_url_fopen"选项,set_time_limit()函数控制程序运行时间可避免超时错误。
九、移动文件指针
1.ftell(handle) 返回文件指针当前位置
2.fseek(handle,offset[,whence]) 移动文件指针到由offset参数指定位置。
参数三:SEEK_CUR 当前位置加上第二个参数把提供的字节;
SEEK_END EOF加上offset字节,此时,offset必须为负值;
SEEK_SET offset字节处,与无此参数效果相同;
成功返回0,失败返回-1。如以a或a+打开,总是附加在后面,不管文件指针位置。
3.rewind(handle) 移动到文件开关
十、文件锁定机制(防止多用户同时访问同一文件造成文件混乱)
1.flock(handle,operation[,&wouldblock]) 文件锁定操作,参数二:LOCK_SH 共享锁定,读取数据时使用;LOCK_EX 独占锁定,写入数据使用;LOCK_UN 释放锁定;LOCK_NB 附加锁定,防止锁定时堵塞。参数三:设为1时,锁定期间阻止其他进程。
十、文件复制、删除等
1.copy(本源文件,目的文件) 复制
2.unlink(目标文件) 删除文件
3.ftruncate(目标文件资源,截取长度) 将文件截断到指定长度
4.rename(旧文件名,新文件名) 重命名文件或目录
十一、文件上传与下载
1.全局数组$_FILES
$_FILES["myfile"]["name"] 原名称,含扩展名
$_FILES["myfile"]["size"] 已上传文件大小,单位为字节
$_FILES["myfile"]["tmp_name"] 上传后临时文件名
$_FILES["myfile"]["error"] 0:成功;1:大小超出PHP配置文件限制;2:大小超出表单限制;3:文件上载不完整;4:没有上载任何文件
$_FILES["myfile"]["type"] 获取上传文件的MIME类型
2.is_uploaded_file() 判断是否是通过HTTPPOST上传的
3.move_uploaded_file() 将上传的文件从临时位置移动到新位置
4.下载头信息处理
header('Content-Type:image/gif'); MIME类型
header('Content-Disposition:attachment;filename="test.gif"'); 头信息,附件和文件名
header('Content-Length:3390'); 大小
readfile('test.gif');
原文地址:http://www.software8.co/wzjs/PHPshili/897.html

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft