PHP file management and implementation of basic functions

迷茫
Release: 2023-03-06 22:16:01
Original
1524 people have browsed it

Basic operations of files

##Let’s first look at the basic operations of PHP files, please see the powerful comments

<body>
Copy after login
<?php
var_dump(filetype("./img/11.png"));
//判断返回得是文件还是目录,返回sile为文件,dir为目录(文件夹)

var_dump(is_dir("./img/11.png"));
//判断给的文件是不是一个目录,目录为ture,文件为false

var_dump(is_file("./img"));
//判断是否为文件,同上

var_dump(date("Y-m-d H:i:s",fileatime("./img/11.png")));
//上次访问时间

var_dump(date("Y-m-d H:i:s",filectime("./img/11.png")));
//创建时间
var_dump(date("Y-m-d H:i:s",filemtime("./img/11.png")));
//修改时间

var_dump(filesize("./img/11.png"));
//获取文件大小

var_dump(file_exists("/QQPCMgr/www/wenjian/img/22.png"));
//在php里面根/则是磁盘

echo $_SERVER[&#39;DOCUMENT_ROOT&#39;];
//获取到服务器根路径

echo basename("/QQPCMgr/www/wenjian/img/22.png");
//返回22.png带后缀的文件名

echo basename("/QQPCMgr/www/wenjian/img/22.png",".png");
//扔上后缀之后只显示文件名(获取文件名)

echo dirname("/QQPCMgr/www/wenjian/img/22.png");
//返回的是不包含文件名的路径(获取文件名以上的)

var_dump(pathinfo("/QQPCMgr/www/wenjian/img/22.png"));
//这个获取的很全面,都能获取到

echo realpath("./img/11.png");
//真实路径:可以把相对路径转换为绝对路径

var_dump(glob("./ce/*"));
//取到这个文件夹里所有的文件,加后缀为条件

 ?>
Copy after login
<!--<img src="/wenjian/img/11.png" />--><!--在网页里根/代表的是www目录--></body>
Copy after login

Overall file operation:

<?php//touch("./11.txt");
//创建文件

//copy("11.txt","./ce/11.txt");
//复制文件

//unlink("./11.txt");
//删除文件

//echo file_get_contents("./ce/11.txt");本地
//echo file_get_contents("http://www.baidu.com");远程
//读取文件所有内容

//file_put_contents("./11.txt","Myshao");
//往文件里面存储内容

//readfile("./11.txt");
//读取并输出

//$arr = file("./shouye.php");
//var_dump($arr);
//读取文件内所有内容,并扔到数组显示

//$ff = fopen("./11.txt","a");
//打开文件资源,详情见注1;

//echo fgetc($ff);
//读取一个字符
//echo fgets($ff);
//读取一行字符
//echo fread($ff,2);
//规定读多长
//fwrite($ff,"shao");
//写入内容
Copy after login

Note 1: Open and read files

##php uses the fopen() function, the syntax structure is as follows

##Resource fopen (string $filename,string $mode) Filename is the target File name, you can open a local file or a remote file. To open a remote file, you need to use the http://... format. If the target file is on the

ftp server , the form ftp://... is used.

The parameter mode is the target file opening mode, and the parameter $mode is the acceptable mode.

File opening method table:

## Opening and closing of directory resources: It is relevant if it is opened, otherwise it will affect subsequent deletion and other operations;

<?php
$fname = "./ce/gf";$d = opendir($fname);//打开文件资源
while ($url = readdir($d))
{    echo $fname."/".$url."<br/>";//    仅读取文件名,把路径拼上=完整路径}var_dump(glob("./*"));closedir($d);//关资源
Copy after login
The above are some basic statements, let’s do some exercises:

Example: Return the number of all files in a folder;

If you want to calculate how many files there are in the ajax directory, you can use the encapsulated method shu() below to traverse the directory and calculate other files in the ce directory. The total number of files in the folder,

<?php
function shu($url)
{    $sl = 0;    $arr = glob($url);    //循环遍历
    foreach($arr as $v)
    {        //判断是不是一个文件
        if(is_file($v))
        {            //如果是一个文件+1
            $sl++;
        }        else
        {            $sl +=shu($v."/*");
        }
    }    return $sl;
}echo shu("./ce/*");//最后给方法一个路径进行查找?>
Copy after login
Look at the output:

One more!

Example:

Delete file

<?php
$fname = "./ce/gf";$d = opendir($fname);//打开文件资源
while ($url = readdir($d))
{    echo $fname."/".$url."<br/>";//    仅读取文件名,把路径拼上=完整路径}var_dump(glob("./*"));closedir($d);//关资源

//删除文件夹(非空文件夹)function shan($url)
{//    清空文件夹
    $d = opendir($url);//    打开
    while ($u = readdir($d))//$u现在是文件名    {//        排除...
        if($u!="." && $u!="..")
        {            $fname = $url . "/" . $u;            //完整带路径的文件名
            if (is_file($fname))
            //如果是一个文件            
            {                
            unlink($fname);
            } else //如果是一个文件夹            {
                shan($fname);
            }
        }
    }    closedir($d);    //关闭

    rmdir($url);
}
shan("./122");?>
Copy after login
In this way, everything in the 122 directory, whether it is a folder or a file, will be deleted;

Implement the file management function

1. First make the function of viewing files and let it display all files and folders. ;

<body>
{$name}

"; }?>
Copy after login
Picture:

##Next, give the folder a special display:

You need to judge before outputting to determine whether it is a folder. :

//从完整路径中取文件名
   $name = basename($v);   if(is_dir($v)){
       echo "<p class=&#39;item dir&#39; url=&#39;{$v}&#39;>{$name}</p>";
   }   else {
       echo "<p class=&#39;item&#39; url=&#39;{$v}&#39;>{$name}</p>";
   }
Copy after login
If it is a folder, just change the background color

picture:

2.给文件夹添加双击事件

双击实现进入这个目录;

js代码:

<script>
    $(".dir").dblclick(function(){        
    var url = $(this).attr("url");
        $.ajax({
            url:"chuli.php",
            data:{url:url},
            type:"POST",
            dataType:"TEXT",
            success:function(data)
            {

                window.location.href="wenwen.php";

            }

        });
    })</script>
Copy after login

处理页面:

<?phpsession_start();
$url = $_POST["url"];
$_SESSION["fname"] = $url;
Copy after login

这样就可以实现双击进入此文件夹:

3.返回上一级,找到上一级目录,写个p

$pname = dirname($fname);
echo "<p id=&#39;shang&#39; url=&#39;{$pname}&#39;>返回上一级</p>";
Copy after login

图:

写双击事件:

<script>
 $("#shang").dblclick(function(){       
  var url = $(this).attr("url");
        $.ajax({
            url:"chuli.php",
            data:{url:url},
            type:"POST",
            dataType:"TEXT",
            success:function(data)
            {
                window.location.href="wenwen.php";
            }
        });
    })</script>
Copy after login

返回到文件目录后使其隐藏:

//上一级的目录$pname = dirname($fname);
if(realpath($fname)=="F:\\QQPCMgr\\WWW\\wenjian"){

}else {
    echo "<p id=&#39;shang&#39; url=&#39;{$pname}&#39;>返回上一级</p>";
}
Copy after login

这样的话当我返回到wenjian目录的时候,使其隐藏:

4.删除功能

在文件p里面加删除按钮

 echo "<p class=&#39;item&#39; url=&#39;{$v}&#39;>{$name}<input type=&#39;button&#39; value=&#39;删除&#39; url=&#39;{$v}&#39; class=&#39;sc&#39;/></p>";
Copy after login

来写按钮的点击事件:

js代码:

$(".sc").click(function(){           
 //确认删除提示
            var av = confirm("确定要删除");            
            if(av){            
            var url = $(this).attr("url");
            $.ajax({
                url: "shan.php",
                data: {url: url},
                type: "POST",
                dataType: "TEXT",
                success: function (data) {
                    window.location.href = "wenwen.php";

                }

            });
            }
        })
Copy after login

删除的处理页面:

<?php$url = $_POST["url"];
unlink($url);
Copy after login

这样完成后,当我点击删除:

再点击确定,即可删除

总代码:

管理查看页面:




    
    无标题文档
    
    

<body>
返回上一级

"; }//便利目录下的所有文件显示$arr = glob($fname."/*");foreach ($arr as $v) { //从完整路径中取文件名 $name = basename($v); if(is_dir($v)){ echo "

{$name}

"; } else { echo "

{$name}

"; } }?> <script> $(".dir").dblclick(function(){ var url = $(this).attr("url"); $.ajax({ url:"chuli.php", data:{url:url}, type:"POST", dataType:"TEXT", success:function(data) { window.location.href="wenwen.php"; } }); }) $("#shang").dblclick(function(){ var url = $(this).attr("url"); $.ajax({ url:"chuli.php", data:{url:url}, type:"POST", dataType:"TEXT", success:function(data) { window.location.href="wenwen.php"; } }); }) $(".sc").click(function(){ //确认删除提示 var av = confirm("确定要删除"); if(av){ var url = $(this).attr("url"); $.ajax({ url: "shan.php", data: {url: url}, type: "POST", dataType: "TEXT", success: function (data) { window.location.href = "wenwen.php"; } }); } })</script>
Copy after login

处理:

<?php
session_start();
$url = $_POST["url"];
$_SESSION["fname"] = $url;
Copy after login

删除:

<?php
$url = $_POST["url"];
unlink($url);
Copy after login

The above is the detailed content of PHP file management and implementation of basic functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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!