


How to implement file management and basic function operations in PHP
这篇文章通过实例代码给大家讲解了php文件管理与基础功能的实现,非常不错,具有参考借鉴价值,需要的朋友参考下
文件的基本操作
先来看一下PHP文件基础操作,请看强大注释
<body> <?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['DOCUMENT_ROOT']; //获取到服务器根路径 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/*")); //取到这个文件夹里所有的文件,加后缀为条件 ?> <!--<img src="/static/imghwm/default1.png" data-src="/wenjian/img/11.png" class="lazy" / alt="How to implement file management and basic function operations in PHP" >--> <!--在网页里根/代表的是www目录--> </body>
文件整体操作:
<?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"); //写入内容
注1:打开和读取文件
php使用fopen()函数的方式,语法结构如下
Resource fopen (string $filename,string $mode)
Filename是目标文件名,打开本地文件也可以打开远程文件,打开远程文件需要采用http://...形式,假如目标文件在
ftp服务器上,则采用形式ftp://...。
参数mode是目标文件打开形式,参数$mode是可以接收的模式。
文件打开方式表:
目录资源的打开与关闭:但凡有开就有关,否则会影响到后面的删除等操作;
<?php $fname = "./ce/gf"; $d = opendir($fname); //打开文件资源 while ($url = readdir($d)) { echo $fname."/".$url."<br/>"; // 仅读取文件名,把路径拼上=完整路径 } var_dump(glob("./*")); closedir($d); //关资源
以上就是一些基础的语句了,来做点练习:
例:返回一个文件夹下的所有文件数量;
如果想要计算出ajax目录下有多少的文件,可以用下面封装的方法shu()来遍历目录,可以计算出ce目录下其他的文件夹里面的文件的总和,
<?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/*"); //最后给方法一个路径进行查找 ?>
看一下输出:
再来一个!
例:删除文件
<?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"); ?>
这样122目录里面所有的东西,不管是文件夹还是文件都会被删除;
实现文件管理功能
1.先把查看文件的功能做出来,让他把所有的文件与文件夹啊显示出来;
<body> <?php //定义文件目录 $fname = "./ce"; //便利目录下的所有文件显示 $arr = glob($fname."/*"); foreach ($arr as $v) { //从完整路径中取文件名 $name = basename($v); echo "<p class='item' url='{$v}'>{$name}</p>"; } ?> </body>
图:
接下来给文件夹特殊显示一下把:
输出之前需要判断,判断是不是一个文件夹:
//从完整路径中取文件名 $name = basename($v); if(is_dir($v)){ echo "<p class='item dir' url='{$v}'>{$name}</p>"; } else { echo "<p class='item' url='{$v}'>{$name}</p>"; }
如果是个文件夹,给他背景颜色改变一下就好啦
图:
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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); }) </script>
处理页面:
<?php session_start(); $url = $_POST["url"]; $_SESSION["fname"] = $url;
这样就可以实现双击进入此文件夹:
3.返回上一级,找到上一级目录,写个p
$pname = dirname($fname); echo "<p id='shang' url='{$pname}'>返回上一级</p>";
图:
写双击事件:
<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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); }) </script>
返回到文件目录后使其隐藏:
//上一级的目录 $pname = dirname($fname); if(realpath($fname)=="F:\\QQPCMgr\\WWW\\wenjian") {} else { echo "<p id='shang' url='{$pname}'>返回上一级</p>"; }
这样的话当我返回到wenjian目录的时候,使其隐藏:
4.删除功能
在文件p里面加删除按钮:
echo "<p class='item' url='{$v}'>{$name} <input type='button' value='删除' url='{$v}' class='sc'/> </p>";
来写按钮的点击事件:
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"; } }); } })
删除的处理页面:
<?php $url = $_POST["url"]; unlink($url);
这样完成后,当我点击删除:
再点击确定,即可删除
总代码:
管理查看页面:
无标题文档 {$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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); }) $("#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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); }) $(".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>
处理:
<?php session_start(); $url = $_POST["url"]; $_SESSION["fname"] = $url;
删除:
<?php $url = $_POST["url"]; unlink($url);
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
How to implement message boardfunction in php
##
The above is the detailed content of How to implement file management and basic function operations in PHP. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool