PHP's Simple File management (basic function)

Source: Internet
Author: User

(1) What file do you want to work on first?

(2) Determine the path of the file?

(3) What file management functions are needed?

First, do a simple look at the file features, files and folders are displayed, but double-click the folder can display the next level of subdirectories, double-click Back to the previous level to return to the previous level of the directory

(1) First, you need to manage the file traversal, you can add a style

<?php//Open Directory Resource $dir = Opendir ($fname);  Loop read while ($n = Readdir ($dir))   {  
To spell a path $url = $fname. " /". $n;
Without this condition, there will be.    This path if ($n! = "." && $n! = "...") {if (Is_dir ($url)) //Determine if the path is not a directory {echo "<div class= ' file Mulu ' lj= ' {$url} ' >{$n}</div>"; If it is a directory, enter this, a name, double-click this to enter the next subdirectory} else {echo "<div class= ' file ' lj= ' {$url} ' >{$n}</div>"; If it's not a directory, enter this. A unified name, so that they write style}}} closedir ($dir); Close Directory Resource?>

To the traversal of the directory and files to add style, the above will give them a name (style can be changed casually)

<style type= "Text/css" >*{margin:0px auto; padding:0px}.file{width:50%; height:30px; border:1px Solid #60F}.mulu {background-color: #639; color: #FFF}        </style>

Look at the result style: With the background color is the directory folder, representing the following files, you need to double-click the event to open the next step; no background color is pure file

(2) Add an event to the directory in the traversed file (double-click the directory to open the subdirectory)

<script type= "Text/javascript" >
Locate the directory, and then double-click the event $ (". Mulu"). DblClick (function () { var url = $ (this). attr ("LJ"); Click this directory to find the path to this directory $.ajax ({ URL: "wjcl.php",// Write Process page Data:{url:url}, //route the path to the processing page type: "POST", // Method datatype: "TEXT", success:function (data) {window.location.href = "file_homework.php"; Refresh page})}) </script>

(3) The processing of the page writing (can not use post or get to pass value, the session will be passed the value of the value)

<?phpsession_start ();  Open Session$url = $_post["url"];  Receive the URL value passed above $_session["url"]= $url; The value of Session mode equals the URL value passed

(4) After the end of the processing page, also to open the session on the main page, but also to determine whether the value is empty, because just start to manage the page does not "return to the upper level", so to judge

To write at the top of PHP session_start ();//This is written in "Open Directory resource Above" if (!empty ($_session["url")) {    $fname = $_session["url"];}

(5) Output "Back to top level" div

$JDLJ = Realpath ($fname);  The absolute path of the file if ($JDLJ = "D:\\wamp\\www\\ajax") {  //If the path is equal to an empty}else{$fuji = dirname ($fname);//Otherwise the path with no file name is output echo "< Div class= ' file prev ' lj= ' {$fuji} ' > Return to Top </div> ';  Output this div}

Add some style to this div.

. prev{background-color: #09F; color: #906}

Look at the results:

The first time you open it, there is no "return to the previous level" column.

When I double-click any folder, I go to its subdirectory and the "Back to Top" column appears.

(6) Now that you have this column, you can use this column to return to the previous level of the table of contents, we can give this div double-click event, let it return

$ (". Prev"). DblClick (function () {var url = $ (this). attr ("LJ");  Find this div.
The processing page when processing the page and traversing the directory is the same $.ajax ({ URL: "wjcl.php", Data:{url:url}, type: "POST", dataType: "TEXT", success:function (data) {window.location.href = "file_homework.php"; Refresh page}) })

This allows you to double-click to return to the top level directory.

Second, further management of the file, you can create a new file in this folder

(1) Just write a text box and add a New button

<input type= "text" id= "name"/>  //text box: <input type= "button" value= "new" id= "new"/>  //New button with Name

(2) Write Click event for button

$ ("#new"). Click (function () {    var name = $ ("#name"). Val ();    $.ajax ({        URL: "newcl.php",        data:{name:name},        type: "POST",        dataType: "TEXT",        success:function (d) {window.location.href = "file_homework.php";   Refresh page})    })          

(3) Processing page for new file

<?phpsession_start ();  Open session$name = $_post["name"];  Gets the name of the pass-through named "url" of $_session["];  Saved path location $filename = $url. " /". $name;  Spell Save Path Touch ($filename);  New file Method Touch ()?>

The results demonstrate:

Select a new location and enter the filename::: Create a new you.php file that displays:

Third, the next management function, delete the unwanted files

(1) Add Delete button, in traverse file that add Delete button

echo "<div class= ' file ' lj= ' {$url} ' >{$n}<input type= ' button ' value= ' delete ' lj= ' {$url} ' id= ' delete '/></div > ";//a name, give him a style, you can make him look a little bit, but also for the following Add event

The added style:

#delete {float:right}   //floating

Remove the display effect when the button is added

(2) Add a click event to a button

$ (". Delete"). Click (function () {var LJ = $ (this). attr ("LJ");  Locate the path of the deleted file $.ajax ({url: "deletecl.php",  //write the deleted processing page data:{lj:lj},type: "POST", DataType: "TEXT", Success: function (a) {window.location.href = "file_homework.php";  Refresh page})})

(3) Delete the page is very simple, is to find this value, delete the file method can be deleted

<?PHP$LJ = $_post["LJ"];unlink ($LJ);  Methods for deleting Files unlink ()?>

Delete also finished, see the effect, then delete the new file we created before it

The file has been deleted ~ ~

In addition to these basic functions, you can also have a lot of file management functions, followed by the update in ....

PHP's Simple File management (basic function)

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.