请记住投稿邮箱是:203239351@qq.com   注册联系QQ203239351

php判断文件是否存在|处理文件大小|文件下载|读取一个字符等功能函数代码

php.png

判断文件是否存在函数:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
       $filename="C:/xampp/htdocs/xx/1.txt";
       if(file_exists($filename)){
           echo "文件存在";
       }else{
           echo "文件不存在";
       }
        ?>
    </body>
</html>

处理文件大小函数:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $filename="C:/xampp/htdocs/xx/1.txt";
        $num=filesize($filename);
        echo($num/1024)."KB";
        ?>
    </body>
</html>

文件的下载功能:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $textname=$_SERVER['DOCUMENT_ROOT']."htdocs/xx/1.txt";
        header("content-type:text/plain");
        header("content-length:".filesize($textname));
        header("content-disposition:attachment;filename=$textname");
        readfile($textname);
        ?>
    </body>
</html>

读取一个字符:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $handle=fopen("1.txt","r");
                while(!feof($handle)){
            $char=fgetc($handle);
            echo($char=="/n"?'<br>':$char);
                }
        ?>
    </body>
</html>


宗波尘客(www.zbck123.com)宗九尘博客     联系QQ:203239351