Sample code sharing for php file management and basic function operations (Figure)

Source: Internet
Author: User
Tags glob
This article explains the implementation of PHP file management and basic functions through the instance code, which is very good and has reference value, for more information about php file management and basic functions, see this article.

Basic file operations

First, let's take a look at the basic operations of the PHP file. see the powerful comments.

 
 
 

File operations:

 

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;

  "; // Read only the file name, put 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,

  

Let's take a look at the output:

Another one!

Example: delete an object

  "; // 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;

  {$ Name}

";}?>

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"

{$ Name}

";} Else {echo"

{$ Name}

";}

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:

  

In this way, you can double-click to enter this folder:

3. return to the upper level, locate the upper level directory, and write a p

$ Pname = dirname ($ fname); echo"

Back to level 1

";

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"

Back to level 1

";}

In this way, when I return to the wenjian Directory, hide it:

4. Deletion

Add the delete button to file p:

Echo"

{$ Name}

";

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:

   

After this is done, click delete:

Click OK to delete the file.

Total code:

Management View page:

 
     Untitled Document 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.