PHP常用函数总结(180多个)

2019-05-02 07:19:53于丽

输出: 读入一个文件并写入到输出缓冲。返回从文件中读入的字节数。如果出错返回 FALSE

135.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 ]]]] )   136.file_put_contents():将一个字符串写入文件

    file_put_contents('1.txt','aa');
调用: int file_put_contents ( string $filename , string $data [, int $flags [, resource $context ]] )

输出: 该函数将返回写入到文件内数据的字节数

137.ftell(): 返回文件指针读/写的位置

 $fp=fopen('tx.txt','r');
 fseek($fp,10);
 echo ftell($fp);
 fread($fp,4);
 echo ftell($fp);

调用: int ftell ( resource $handle ) 输出: 返回由 handle 指定的文件指针的位置,也就是文件流中的偏移量

138.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

139.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

140.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

目录

141.basename(): 返回路径中的文件名部分

 path = "/home/httpd/html/index.php";
 $file = basename($path);
 $file = basename($path,".php");

调用: string basename ( string $path [, string $suffix ]) 输出: 给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。如果文件名是以 suffix 结 束的,那这一部分也会被去掉

142.dirname(): 返回路径中的目录部分

 $path = "/etc/passwd";
 $file = dirname($path);

调用: string dirname ( string $path ) 输出: 给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名

143.pathinfo(): 返回文件路径的信息

 echo '<pre>';
 print_r(pathinfo("/www/htdocs/index.html"));
 echo '</pre>';

调用: mixed pathinfo ( string $path [, int $options ] ) 返回一个关联数组包含有 path 的信息

相关文章 大家在看