php常用函数总结2
| 文件系统函数 | |||||
| 函数名 | 描述 | 实例 | 输入 | 输出 | 操作 |
| fopen() | 打开文件或者 URL | $handle = fopen("ftp://user:password@example.com/somefile.txt", "w"); | resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] ) | 如果打开失败,本函数返回 FALSE | |
| fclose() | 关闭一个已打开的文件指针 | $handle = fopen('somefile.txt', 'r'); fclose($handle); |
bool fclose(resource handle) |
如果成功则返回 TRUE,失败则返回 FALSE |
|
| 文件属性 | |||||
| file_exists() | 检查文件或目录是否存在 | $filename = '/path/to/foo.txt'; if (file_exists($filename)) { echo "exists"; } else { echo "does not exist"; } |
bool file_exists ( string filename ) |
指定的文件或目录存在则返回 TRUE,否则返回 FALSE |
|
| filesize() | 取得文件大小 | $filename = 'somefile.txt'; echo $filename . ': ' . filesize($filename) . 'bytes'; |
int filesize ( string $filename ) |
返回文件大小的字节数,如果出错返回 FALSE 并生成一条 E_WARNING 级的错误 |
|
| is_readable() | 判断给定文件是否可读 | $filename = 'test.txt'; if (is_readable($filename)) { echo '可读'; } else { echo '不可读'; } |
bool is_readable ( string $filename ) |
如果由 filename 指定的文件或目录存在并且可读则返回 TRUE |
|
| is_writable() | 判断给定文件是否可写 | $filename = 'test.txt'; if (is_writable($filename)) { echo '可写'; } else { echo '不可写'; } |
bool is_writable ( string $filename ) |
如果文件存在并且可写则返回 TRUE。filename 参数可以是一个允许进行是否可写检查的目录名 |
同名函数is_writable() |
| is_executable() | 判断给定文件是否可执行 | $file = 'setup.exe'; if (is_executable($file)) { echo '可执行'; } else { echo '不可执行'; } |
bool is_executable ( string $filename ) |
如果文件存在且可执行则返回 TRUE |
|
| filectime() | 获取文件的创建时间 | $filename = 'somefile.txt'; echo filectime($filename); |
int filectime ( string $filename ) |
时间以 Unix 时间戳的方式返回,如果出错则返回 FALSE |
|
| filemtime() | 获取文件的修改时间 | $filename = 'somefile.txt'; echo filemtime($filename); |
int filemtime ( string $filename ) |
返回文件上次被修改的时间,出错时返回 FALSE。时间以 Unix时间戳的方式返回 |
|
| fileatime() | 获取文件的上次访问时间 | $filename = 'somefile.txt'; echo fileatime($filename); |
int fileatime (string $filename) |
返回文件上次被访问的时间,如果出错则返回 FALSE。时间以Unix时间戳的方式返回 |
|
| stat() | 获取文件大部分属性值 | $filename = 'somefile.txt'; var_dump(fileatime($filename)); |
array stat (string $filename) |
返回由 filename 指定的文件的统计信息 |
|
| 文件操作 | |||||
| fwrite() | 写入文件 | $filename = 'test.txt'; $somecontent = "添加这些文字到文件\n"; $handle = fopen($filename, 'a'); fwrite($handle, $somecontent); fclose($handle); |
int fwrite ( resource handle, string string [, int length] ) |
把 string 的内容写入 文件指针 handle 处。 如果指定了 length ,当写入了 length 个字节或者写完了 string 以后,写入就会停止,视乎先碰到哪种情况 |
同名函数 fputs() |
| fputs() | 同上 | 同名函数 fwrite() |
|||
| fread() | 读取文件 | $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize ($filename)); fclose($handle); |
string fread ( int handle, int length )从文件指针 handle,读取最多 length 个字节 |
从文件指针 handle 读取最多 length 个字节 |
|
| feof() | 检测文件指针是否到了文件结束的位置 | $file = @fopen("no_such_file", "r"); while (!feof($file)) { } fclose($file); |
bool feof ( resource handle ) |
如果文件指针到了 EOF 或者出错时则返回 TRUE,否则返回一个错误(包括 socket 超时),其它情况则返回 FALSE |
|
| fgets() | 从文件指针中读取一行 | $handle = @fopen("/tmp/inputfile.txt", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); echo $buffer; } fclose($handle); } |
string fgets ( int handle [, int length] ) |
从 handle 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。 |
|
| fgetc() | 从文件指针中读取字符 | $fp = fopen('somefile.txt', 'r'); if (!$fp) { echo 'Could not open file somefile.txt'; } while (false !== ($char = fgetc($fp))) { echo "$char\n"; } |
string fgetc ( resource $handle ) |
返回一个包含有一个字符的字符串,该字符从 handle 指向的文件中得到。碰到 EOF 则返回 FALSE |
|
| file() | 把整个文件读入一个数组中 | ################################ | array file ( string $filename [, int $use_include_path [, resource $context ]] ) |
数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败 file() 返回 FALSE |
|
| readfile() | 输出一个文件 | int readfile ( string $filename [, bool $use_include_path [, resource $context ]] ) |
读入一个文件并写入到输出缓冲。返回从文件中读入的字节数。如果出错返回 FALSE | ||
| file_get_contents() | 将整个文件读入一个字符串 | echo file_get_contents('http://www.baidu.com'); |
string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] ) |
||
| file_put_contents() | 将一个字符串写入文件 | file_put_contents('1.txt','aa'); | int file_put_contents ( string $filename , string $data [, int $flags [, resource $context ]] ) |
该函数将返回写入到文件内数据的字节数 | |
| ftell() | 返回文件指针读/写的位置 | $fp=fopen('tx.txt','r'); fseek($fp,10); echo ftell($fp); fread($fp,4); echo ftell($fp); |
int ftell ( resource $handle ) |
返回由 handle 指定的文件指针的位置,也就是文件流中的偏移量 |
|
| fseek() | 在文件指针中定位 | $fp=fopen('tx.txt','r'); fseek($fp,10); echo ftell($fp); fread($fp,4); echo ftell($fp); |
int fseek ( resource $handle , int $offset [, int $whence ] ) |
成功则返回 0;否则返回 -1 |
|
| rewind() | 倒回文件指针的位置 | $fp=fopen('tx.txt','r'); fseek($fp,3); echo ftell($fp); fread($fp,4); rewind($fp); echo ftell($fp); |
bool rewind ( resource $handle ) |
如果成功则返回 TRUE,失败则返回 FALSE |
|
| flock() | 轻便的咨询文件锁定 | $fp=fopen('tx.txt','r'); flock($fp, LOCK_SH);//共享锁 //flock($fp, LOCK_EX);//独立锁,写文件时用它打开 //flock($fp, LOCK_NB);//附加锁 flock($fp, LOCK_UN);//释放锁 fclose($fp); |
bool flock ( int $handle , int $operation [, int &$wouldblock ] ) |
如果成功则返回 TRUE,失败则返回 FALSE |
|
| 目录 | |||||
| basename() | 返回路径中的文件名部分 | path = "/home/httpd/html/index.php"; $file = basename($path); $file = basename($path,".php"); |
string basename ( string $path [, string $suffix ] ) |
给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。如果文件名是以 suffix 结束的,那这一部分也会被去掉 |
|
| dirname() | 返回路径中的目录部分 | $path = "/etc/passwd"; $file = dirname($path); |
string dirname ( string $path ) |
给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名 | |
| pathinfo() | 返回文件路径的信息 | echo '<pre>'; print_r(pathinfo("/www/htdocs/index.html")); echo '</pre>'; |
mixed pathinfo ( string $path [, int $options ] ) |
返回一个关联数组包含有 path 的信息 |
|
| opendir() | 打开目录句柄 | $fp=opendir('E:/xampp/htdocs/php/study/19'); echo readdir($fp); closedir($fp); |
resource opendir ( string $path [, resource $context ] ) |
如果成功则返回目录句柄的 resource,失败则返回 FALSE |
|
| readdir() | 从目录句柄中读取条目 | $fp=opendir('E:/xampp/htdocs/php/study/19'); echo readdir($fp); closedir($fp); |
string readdir ( resource $dir_handle ) |
返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回 | |
| closedir() | 关闭目录句柄 | $fp=opendir('E:/xampp/htdocs/php/study/19'); echo readdir($fp); closedir($fp); |
void closedir ( resource $dir_handle ) |
关闭由 dir_handle 指定的目录流。流必须之前被 opendir() 所打开 |
|
| rewinddir() | 倒回目录句柄 | $fp=opendir('E:/xampp/htdocs/php/study/19'); echo readdir($fp).'<br />'; echo readdir($fp).'<br />'; echo readdir($fp).'<br />'; rewinddir($fp); echo readdir($fp).'<br />'; closedir($fp); |
void rewinddir ( resource $dir_handle ) |
将 dir_handle 指定的目录流重置到目录的开头 |
|
| mkdir() | 新建目录 | mkdir('123'); | bool mkdir ( string $pathname [, int $mode [, bool $recursive [, resource $context ]]] ) |
尝试新建一个由 pathname 指定的目录 |
|
| rmdir() | 删除目录 | rmdir('123'); | bool rmdir ( string $dirname ) |
尝试删除 dirname 所指定的目录。 该目录必须是空的,而且要有相应的权限。如果成功则返回 TRUE,失败则返回 FALSE |
|
| unlink() | 删除文件 | unlink('123/1.txt'); rmdir('123'); |
bool unlink ( string $filename ) |
删除 filename 。和 Unix C 的 unlink() 函数相似。如果成功则返回 TRUE,失败则返回 FALSE |
|
| copy() | 拷贝文件 | copy('index.php','index.php.bak'); | bool copy ( string $source , string $dest ) |
将文件从 source 拷贝到 dest 。如果成功则返回 TRUE,失败则返回 FALSE |
|
| rename() | 重命名一个文件或目录 | rename('tx.txt','txt.txt'); | bool rename ( string $oldname , string $newname [, resource $context ] ) |
如果成功则返回 TRUE,失败则返回 FALSE |
|
| 文件的上传与下载 | |||||
| is_uploaded_file() | 判断文件是否是通过 HTTP POST 上传的 |
if(is_uploaded_file($_FILES['bus']['tmp_name'])){ if( move_uploaded_file($_FILES['bus']['tmp_name'], $NewPath) ){ echo '上传成功<br /><img src="'.$NewPath.'">'; }else{ exit('失败'); } }else{ exit('不是上传文件'); } |
bool is_uploaded_file ( string $filename ) |
||
| move_uploaded_file() | 将上传的文件移动到新位置 | if(is_uploaded_file($_FILES['bus']['tmp_name'])){ if( move_uploaded_file($_FILES['bus']['tmp_name'], $NewPath) ){ echo '上传成功<br /><img src="'.$NewPath.'">'; }else{ exit('失败'); } }else{ exit('不是上传文件'); } |
bool move_uploaded_file ( string $filename , string $destination ) |
||
| 时间函数 | |||||
| 函数名 | 描述 | 实例 | 输入 | 输出 | 操作 |
| time() | 返回当前的 Unix 时间戳 | time(); | int time ( void ) | 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数 | |
| mktime() | 取得一个日期的 Unix 时间戳 | mktime(0, 0, 0, 4, 25, 2012); | int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] ) | ||
| date() | 格式化一个本地时间/日期 | date('Y年m月d日 H:i:s'); | string date ( string $format [, int $timestamp ] ) | 2012年04月25日 20:45:54 | |
| checkdate() | 验证一个格里高里日期 | if(checkdate(6,31,2012)){ echo '成立'; }else{ echo '不成立'; } |
bool checkdate ( int $month , int $day , int $year ) |
如果给出的日期有效则返回 TRUE,否则返回 FALSE |
|
| date_default_timezone_set() | 设定用于一个脚本中所有日期时间函数的默认时区 | date_default_timezone_set('PRC'); | bool date_default_timezone_set ( string $timezone_identifier ) |
||
| getdate() | 取得日期/时间信息 | $t=getdate(); var_dump($t); |
array getdate ([ int $timestamp ] ) |
返回一个根据 timestamp 得出的包含有日期信息的结合数组。如果没有给出时间戳则认为是当前本地时间 |
|
| strtotime() | 将任何英文文本的日期时间描述解析为 Unix 时间戳 |
echo strtotime("now"); echo strtotime("10 September 2000"); echo strtotime("+1 day"); echo strtotime("+1 week"); echo strtotime("+1 week 2 days 4 hours 2 seconds"); echo strtotime("next Thursday"); echo strtotime("last Monday"); |
int strtotime ( string $time [, int $now ] ) |
||
| microtime() | 返回当前 Unix 时间戳和微秒数 |
$start=microtime(true); sleep(3); $stop=microtime(true); echo $stop-$start; |
mixed microtime ([ bool $get_as_float ] ) |
||
| 正则表达式-元字符 | ||
| 元字符 | 含义 | 等价于 |
| 匹配范围 | ||
| \d | 匹配任意一个十进制数字 | [0-9] |
| \D | 匹配除十进制数字以外的任意数字 | [^0-9] |
| \s | 匹配空白字符 | [\n\f\r\t\v] |
| \S | 匹配除空白字符以外的任意一个字符 | [^\n\f\r\t\v] |
| \w | 匹配任意一个数字、字母和下划线 | [0-9a-zA-Z_] |
| \W | 匹配除字母、数字和下划线以外的任意字符 | [^0-9a-zA-Z_] |
| [] | 1)用来表示范围。 2)匹配任意一个中括号中定义的原子 | |
| [^] | 中括号里面的^(抑扬符):表示匹配任意一个除中括号里面定义的原子 | |
| 限定次数 | ||
| * | 匹配0次、1次或多次其前的原子 | {0,} |
| + | 匹配1次或多次其前的原子 | {1,} |
| ? | 匹配0次或1次其前的原子 | {0,1} |
| {n} | 表示其前的原子正好出现n次 | |
| {n,} | 表示其前的原子至少出现n次,最多不限制 | |
| {m,n} | 表示其前的原子最少出现m次,最多出现n次 | |
| 其它 | ||
| . | 匹配除换行符(\n)以外的任意字符【windows下还匹配\f\r】 | |
| | | 两个或多个分支选择【优先级最低】 | |
| ^ | 匹配输入字符的开始位置 | |
| $ | 匹配输入字符的结束位置 | |
| \b | 匹配词边界 | |
| \B | 匹配非词边界 | |
| () | 1)模式单元,把多个小原子组成一个大原子。2)可以改变优先级 | |
php常用函数总结2的更多相关文章
- oracle常用函数及示例
学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...
- 总结js常用函数和常用技巧(持续更新)
学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...
- [转]SQL 常用函数及示例
原文地址:http://www.cnblogs.com/canyangfeixue/archive/2013/07/21/3203588.html --SQL 基础-->常用函数 --===== ...
- PHP常用函数、数组方法
常用函数:rand(); 生成随机数rand(0,50); 范围随机数时间:time(); 取当前时间戳date("Y-m-d H:i:s"); Y:年 m:月份 d:天 H:当前 ...
- Oracle常用函数
前一段时间学习Oracle 时做的学习笔记,整理了一下,下面是分享的Oracle常用函数的部分笔记,以后还会分享其他部分的笔记,请大家批评指正. 1.Oracle 数据库中的to_date()函数的使 ...
- Thinkcmf:页面常用函数
Thinkcmf:页面常用函数 全站seo: 文章列表: {$site_seo_title} <!--SEO标题--> {$site_seo_keywords} < ...
- matlab进阶:常用功能的实现,常用函数的说明
常用功能的实现 获取当前脚本所在目录 current_script_dir = fileparts(mfilename('fullpath')); % 结尾不带'/' 常用函数的说明 bsxfun m ...
- iOS导航控制器常用函数与navigationBar常用属性
导航控制器常用函数触发时机 当视图控制器的View将要出现时触发 - (void)viewWillAppear:(BOOL)animated 当视图控制器的View已经出现时触发 - (void)vi ...
- 《zw版·Halcon-delphi系列原创教程》 zw版-Halcon常用函数Top100中文速查手册
<zw版·Halcon-delphi系列原创教程> zw版-Halcon常用函数Top100中文速查手册 Halcon函数库非常庞大,v11版有1900多个算子(函数). 这个Top版,对 ...
- phpcms V9 常用函数 及 代码整理
常用函数 及 常用代码 总结如下 <?php //转换字符串或者数组的编码 str_charset($in_charset, $out_charset, $str_or_arr) //获取菜单 ...
随机推荐
- MySQL数据类型DECIMAL用法详解
MySQL DECIMAL数据类型用于在数据库中存储精确的数值.我们经常将DECIMAL数据类型用于保留准确精确度的列,例如会计系统中的货币数据. 要定义数据类型为DECIMAL的列,请使用以下语法: ...
- 【多线程】无锁编程以及CAS
无锁编程 / lock-free / 非阻塞同步 无锁编程,即不使用锁的情况下实现多线程之间的变量同步,也就是在没有线程被阻塞的情况下实现变量的同步,所以也叫非阻塞同步(Non-blocking Sy ...
- JindoFS解析 - 云上大数据高性能数据湖存储方案
JindoFS背景 计算存储分离是云计算的一种发展趋势,传统的计算存储相互融合的的架构存在一定的问题, 比如在集群扩容的时候存在计算能力和存储能力相互不匹配的问题,用户在某些情况下只需要扩容计算能力或 ...
- 【Dart学习】--Dart之正则表达式相关方法总结
一,部分属性 RegExp exp = new RegExp(r"(\w+)"); 返回正则表达式的哈希码 print(exp.hashCode); 正则表达式是否区分大小写 pr ...
- [CSP-S模拟测试67]题解
时隔多年,终于又有了一套我能改完的题…… A.神炎皇 遇到这种要求整除的题显然拆出gcd 设$d=gcd(a,b)\ \ \ a'=\frac{a}{d} \ \ \ b'=\frac{b}{d}$ ...
- ajax中回调的几个坑
在前端开发中,经常要用ajax去拿后台接口返回的数据,总结几个ajax的回调的常见问题,供大家参考爬坑. 未定义contentType,可能会造成的传入后台的数据乱码,可以加上如下代码在ajax请求中 ...
- C++11中vector的几种遍历方法
假设有这样的一个vector: vector<int> line={1,2,3,4,5,6,7,8,9}; 需要输出vector里的每个元素,主函数如下: void showvec(con ...
- react jsx 中使用 switch case 示例
<div> <span>适用平台:</span> <span>{(() => { switch (currentItems.usePlatform ...
- PHP面试 MySQL的高可扩展和高可用
MySQL的高可扩展和高可用 面试题一 MySQL分表和分区的工作原理,分表和分区的使用场景和优缺点. 分区表的原理 对用户而言,分区表时一个独立的逻辑表,但是底层MySQL将其分成了多个物理子表,这 ...
- ROS编程: 重要的代码优化知识点记录(1)
订阅多个话题并对其进行同步处理 本小节针对在ROS节点中需要订阅两个及两个以上的话题时,需要保持对这两个话题数据的同步,且需要同时接收数据一起处理然后当做参数传入到另一个函数中: 研究背景:reals ...