Code examples for PHP file operations

不言
Release: 2023-04-05 10:00:02
forward
1953 people have browsed it

This article brings you code examples about PHP file operations. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<?php
    #判断文件类型是:文件
    $file=&#39;img/meinv.jpg&#39;;
    echo filetype($file);
?>
Copy after login
<?php
    #判断文件类型是:目录
    $file=&#39;img&#39;;
    echo filetype($file);
?>
Copy after login
<?php
    #判断到底是不是文件:是
    $file=&#39;img/meinv.jpg&#39;;
    echo is_file($file);
?>
Copy after login
<?php
    #判断到底是否存在:是
    $file=&#39;img/meinv.jpg&#39;;
    $b=file_exists($file);
    var_dump($b);
?>
Copy after login
<?php
    #获取文件大小M
    $file=&#39;img/meinv.jpg&#39;;
    $b=filesize($file);
    var_dump($b);
?>
Copy after login
<?php
    #读取三个字节
    $file=&#39;./a.txt&#39;;
    $fp=fopen($file,&#39;r&#39;);
    $str=fread($fp,3);
    echo $str;
?>
Copy after login
<?php
    #读取三个字节
    $file=&#39;./a.txt&#39;;
    $fp=fopen($file,&#39;r&#39;);
    $str=fread($fp,6);#换行的时候隐藏了个\n(占据两个字节)
    echo $str;
?>
Copy after login
<?php
    #获取文件里所有东西
    $file=&#39;a.txt&#39;;
    $size=filesize($file);
    $fp=fopen($file,&#39;r&#39;);
    $str=fread($fp, $size);
    echo $str;
?>
Copy after login
<?php
    $file=&#39;b.txt&#39;;
    $fp=fopen($file,&#39;w&#39;);#生成一个b.txt文件 a:追加文本,w重新写
    $str=&#39;aa1<br>bb<br>cc<br>&#39;;
    fwrite($fp,$str);#往文件里写入字符串
?>
Copy after login
<?php
    $file=&#39;b.txt&#39;;
    unlink($file);#删除文件函数
?>
Copy after login
<?php
    $sfile=&#39;a.txt&#39;;
    $dfile=&#39;aaa.txt&#39;;
    rename($sfile,$dfile);#文件重命名
?>
Copy after login
<?php
    $sfile=&#39;aaa.txt&#39;;
    $dfile=&#39;ccc.txt&#39;;
    copy($sfile, $dfile);#文件复制函数
?>
Copy after login
<?php
    $sfile=&#39;aaa.txt&#39;;
    $dfile=&#39;ddd.txt&#39;;
    copy($sfile, $dfile);#文件复制函数
    #把源文件删除了整合一下就是复制
    #后把源文件删除就是移动
    unlink($sfile);
?>
Copy after login
 <?php
    $sfile=&#39;ccc.txt&#39;;
    readfile($sfile);#直接读取文件里内容并输出函数
?>
Copy after login
<?php
    $sfile=&#39;ccc.txt&#39;;    
    $str=file_get_contents($sfile);#只是读取不输出
?>
Copy after login
<?php
    $sfile=&#39;http://www.baidu.com&#39;;
    $str=file_get_contents($sfile);#只是读取不输出
    echo $str; #把读取的百度网址输出,呈现百度界面
?>
Copy after login

The above is the detailed content of Code examples for PHP file operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!