Home Backend Development PHP Tutorial php资料操作相关函数

php资料操作相关函数

Jun 13, 2016 pm 01:07 PM
false file files quot

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 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)

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to convert php blob to file How to convert php blob to file Mar 16, 2023 am 10:47 AM

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use java's File.getParent() function to get the parent path of the file Use java's File.getParent() function to get the parent path of the file Jul 24, 2023 pm 01:40 PM

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of the file Use java's File.getParentFile() function to get the parent directory of the file Jul 27, 2023 am 11:45 AM

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

How to delete a file or directory using File.delete() method in Java? How to delete a file or directory using File.delete() method in Java? Nov 18, 2023 am 08:02 AM

How to delete a file or directory using File.delete() method in Java? Overview: In Java, we can delete a file or directory using the delete() method of the File class. This method is used to delete the specified file or directory. However, it should be noted that this method can only delete empty directories or files that are not opened by other programs. If file or directory deletion fails, you can find the specific reason by catching IOException. Step 1: Import related packages First, we need

Create multi-level directories using java's File.mkdirs() function Create multi-level directories using java's File.mkdirs() function Jul 24, 2023 am 11:04 AM

Create multi-level directories using Java's File.mkdirs() function In Java, we often need to create folders to store and organize files. Sometimes, we need to create multi-level directories, which are folders containing subfolders. Java provides the mkdirs() function of the File class to implement this function. The File class is a class in Java that handles files and directories. It provides a series of methods for operating files and directories. Among them, the mkdirs() function is a function that creates multi-level directories. Down

See all articles