Php implements file management and basic function operations,
Basic File Operations
First, let's take a look at the basic operations of the PHP file. See the powerful comments.
<Body> <? Phpvar_dump (filetype (". /img/11.png"); // determines whether the returned result is a file or a directory. If the returned result is sile, the returned result is a file, and the dir is a directory (folder) var_dump (is_dir (". /img/11.png"); // determines whether the given file is a directory. The directory is true and the file is falsevar_dump (is_file (". /img "); // determines whether it is a file. Same as var_dump (date (" Y-m-d H: I: s ", fileatime (". /img/11.png"); // last access time var_dump (date ("Y-m-d H: I: s", filectime (". /img/11.png"); // creation time var_dump (date ("Y-m-d H: I: s", filemtime (". /img/11.png"); // modify the time var_dump (filesize (". /img/11.png ")); // Obtain the file size var_dump (file_exists ("/QQPCMgr/www/wenjian/img/22.png ")); // In php, the ROOT/is the disk echo $ _ SERVER ['document _ root']; // obtain the server root path echo basename ("/QQPCMgr/www/wenjian/img/22.png "); // return 22.png file name with suffix echo basename ("/QQPCMgr/www/wenjian/img/22.png ",". png "); // display only the file name (get the file name) after throwing the suffix echo dirname ("/QQPCMgr/www/wenjian/img/22.png "); // return the path that does not contain the file name (obtained above the file name) var_dump (pathinfo ("/QQPCMgr/www/wenjian/img/22.png"); // The obtained result is complete. Echo realpath (". /img/11.png"); // actual path: You can convert the relative path to the absolute path var_dump (glob (". /ce/* "); // get all the files in this folder, and add the suffix as the condition?> <! -- --> <! -- On the webpage, Reagan/Represents the www directory --> </body>
File Operations:
<? Php // touch (". /11.txt"); // create a file // copy ("11.txt ",". /ce/11.txt"); // copy the file // unlink (". /11.txt"); // delete the file // echo file_get_contents (". /ce/11.txt"); Local // echo file_get_contents ("http://www.baidu.com"); remote // read all file content // file_put_contents (". /11.txt", "Myshao"); // store the content in the file // readfile (". /11.txt"); // read and output // $ arr = file (". /shouye. php "); // var_dump ($ arr); // read all the content in the file and throw it to the array display // $ ff = fopen (". /11.txt", "a"); // open the file resource. For details, see note 1; // echo fgetc ($ ff ); // read one character // echo fgets ($ ff); // read a line of characters // echo fread ($ ff, 2 ); // specify the read length // fwrite ($ ff, "shao"); // write the content
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. To open a local file, you can also open a remote file. To open a remote file, you must use http: //....
On the ftp server, ftp: //... is used ://....
The parameter mode is the form of opening the target file, and the parameter $ mode is the mode that can be received.
File opening method table:
Opening and disabling directory resources: any operation is related; otherwise, subsequent deletion and other operations will be affected;
<? Php $ fname = ". /ce/gf "; $ d = opendir ($ fname); // open the file resource while ($ url = readdir ($ d) {echo $ fname. "/". $ url. "<br/>"; // read only the file name and spell the Path = full path} var_dump (glob (". /* "); closedir ($ d); // close the resource
The above are some basic statements for some exercises:
For example, the number of all objects in a folder is returned;
If you want to calculate the number of files under the ajax directory, you can use the shu () method encapsulated below to traverse the directory and calculate the total number of files in other folders under the ce directory,
<? Phpfunction shu ($ url) {$ sl = 0; $ arr = glob ($ url); // cyclically traverse foreach ($ arr as $ v) {// determine if it is a file if (is_file ($ v) {// if it is a file + 1 $ sl ++ ;} else {$ sl + = shu ($ v. "/*") ;}}return $ sl;} echo shu (". /ce/* "); // Finally, find a path for the method?>
Let's take a look at the output:
Another one!
Example: delete an object
<? Php $ fname = ". /ce/gf "; $ d = opendir ($ fname); // open the file resource while ($ url = readdir ($ d) {echo $ fname. "/". $ url. "<br/>"; // read only the file name and spell the Path = full path} var_dump (glob (". /* "); closedir ($ d); // close the resource // delete a folder (non-empty folder) function shan ($ url) {// clear the folder $ d = opendir ($ url); // open while ($ u = readdir ($ d )) // $ u is now the file name {// exclude... if ($ u! = "." & $ U! = ".. ") {$ Fname = $ url. "/". $ u; // complete file name with path if (is_file ($ fname) // if it is a file {unlink ($ fname );} else // if it is a folder {shan ($ fname) ;}} closedir ($ d); // close rmdir ($ url);} shan (". /122 ");?>
In this way, everything in the 122 directory, whether in folders or files, will be deleted;
Implement File Management
1. First, display all the files and folders;
<Body> <? Php // define the file directory $ fname = ". /ce "; // all files in the convenient directory are displayed $ arr = glob ($ fname. "/*"); foreach ($ arr as $ v) {// retrieve the file name from the full path $ name = basename ($ v ); echo "<div class = 'item' url = '{$ v}'> {$ name} </div>" ;}?> </Body>
Figure:
Next, we will show the folder as follows:
Before output, you need to determine whether it is a folder:
// Get the file name $ name = basename ($ v) from the complete path; if (is_dir ($ v )) {echo "<div class = 'item dir' url = '{$ v}' >{$ name} </div> ";} else {echo "<div class = 'item' url = '{$ v}' >{$ name} </div> ";}
If it's a folder, just change the background color.
Figure:
2. Add a double-click event to the folder:
Double-click to enter the directory;
Js Code:
<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>
Processing page:
<?phpsession_start();$url = $_POST["url"];$_SESSION["fname"] = $url;
In this way, you can double-click to enter this folder:
3. Return to the upper level, find the upper level directory, and write a div
$ Pname = dirname ($ fname); echo "<div id = 'shanghai' url = '{$ pname}'> returns the previous level </div> ";
Figure:
Double-click event:
<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>
Hide it after returning it to the file directory:
// The directory at the upper level $ pname = dirname ($ fname); if (realpath ($ fname) = "F: \ QQPCMgr \ WWW \ wenjian ") {} else {echo "<div id = 'shanghai' url = '{$ pname}'> back to the upper level </div> ";}
In this way, when I return to the wenjian directory, hide it:
4. Deletion
Add the delete button to the file div:
Echo "<div class = 'item' url = '{$ v}' >{$ name} <input type = 'button 'value = 'delete' url = '{$ v} 'class = 'scs'/> </div> ";
Click Event to write the button:
Js Code:
$ (". SC "). click (function () {// confirm the deletion prompt var av = confirm ("OK to delete"); 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 ";}});}})
Delete processing page:
<?php$url = $_POST["url"];unlink($url);
After this is done, click Delete:
Click OK to delete the file.
Total code:
Management view page:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Processing:
<?phpsession_start();$url = $_POST["url"];$_SESSION["fname"] = $url;
Delete:
<?php$url = $_POST["url"];unlink($url);
The above section describes how to implement file management and basic functions in php. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!