Simple PHP file management functions,
(1) first think about which file to operate?
(2) determine the file path?
(3) What file management functions do you need?
1. Do a simple job of viewing files. The files and folders in the files are displayed, but double-click the folder to display the subdirectories at the next level. Double-click "back to the previous level" to return to the directory at the upper level.
(1) traverse the files to be managed and add a style.
<? Php // open the directory resource $ dir = opendir ($ fname); // read while cyclically ($ n = readdir ($ dir )) {<br> // specify the path $ url = $ fname. "/". $ n; <br> // if this condition is not met, there will be. /.. if ($ n! = "." & $ N! = ".. ") {If (is_dir ($ url )) // determine whether the path is a directory {echo "<div class = 'file mulu 'lj = '{$ url}' >{$ n} </div> "; // if it is a directory, enter this and name it, double-click this to enter the next subdirectory} else {echo "<div class = 'file' lj = '{$ url}' >{$ n} </div> "; // enter this directory if it is not a directory .. Set a uniform name so that you can write styles to them. }}} closedir ($ dir); // close directory resources?>
Add a style to the retrieved directory and file. The above name will be given to them (style can be changed at Will)
<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>
View the result style: the directory folder with the background color indicates that there are files below. You need to double-click the event to open it next. The files without the background color are pure files.
(2) Add an event to the directory in the retrieved file (double-click this directory to open the subdirectory)
<Script type = "text/javascript"> <br> // locate the Directory and double-click the event $ (". mulu "). dblclick (function () {var url = $ (this ). attr ("lj"); // click the directory to find the path $. ajax ({url: "wjcl. php ", // write the processing page data: {url: url}, // upload the path to the processing page type:" POST ", // dataType:" TEXT ", success: function (data) {window. location. href = "file_homework.php"; // refresh page }})) </script>
(3) Compile the processing page (post or get cannot be used here to pass the value. You can use session to pass the value)
<? Phpsession_start (); // enable session $ url =$ _ POST ["url"]; // receives the uploaded url value $ _ SESSION ["url"] = $ url; // The value in session mode is equal to the uploaded url value.
(4) After the processing page ends, you must enable the session on the home page and determine whether the pass value is null. Because the page is not "returned to the upper level" at the beginning of management, you must determine
// Write session_start () at the top of php; // write it on "Open Directory resource" if (! Empty ($ _ SESSION ["url"]) {$ fname = $ _ SESSION ["url"];}
(5) output the div "back to the upper level"
$ Jdlj = realpath ($ fname); // the absolute path of the file if ($ jdlj = "D: \ wamp \ www \ ajax ") {// null if the path is equal} else {$ fuji = dirname ($ fname ); // otherwise, echo "<div class = 'file prev' lj = '{$ fuji}'> returns the previous level </div> "; // output this div}
Add a style to this div
. Prev {background-color: # 09F; color: #906}
Check the result:
The first time you open it, there is no "back to the upper level" column.
When I double-click any folder and enter its subdirectory, the "back to the upper level" column appears.
(6) Now that this column is available, we can use this column to return to the upper-level directory. We can add double-click events to this div so that it can return
$ (". Prev "). dblclick (function () {var url = $ (this ). attr ("lj"); // find this div <br> // The processing page is the same as the processing page when traversing the directory $. ajax ({url: "wjcl. php ", data: {url: url}, type:" POST ", dataType:" TEXT ", success: function (data) {window. location. href = "file_homework.php"; // refresh page }})})
In this way, you can double-click to return to the upper-level directory.
2. Further manage the file. You can create a new file in this folder.
(1) 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
(2) Write a click event to the 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 of the new file
<? Phpsession_start (); // enable session $ name =$ _ POST ["name"]; // obtain the passed name $ url =$ _ SESSION ["url"]; // Save the path $ filename = $ url. "/". $ name; // save path touch ($ filename); // method of File Creation touch ()?>
The result is as follows:
Select a new location and enter the file name: Create a new you. php file:
Iii. Next management function: delete unwanted files
(1) add and delete button to traverse files
Echo "<div class = 'file' lj = '{$ url}' >{$ n} <input type = 'button 'value = 'Delete 'lj =' {$ url} 'id = 'delete'/> </div> "; // create a name and add a style to it to make it look better. It is also used to add events later.
Added style:
# Delete {float: right} // floating again
Display effect after the delete button is added
(2) Add a click event to the button
$ (". Delete "). click (function () {var lj = $ (this ). attr ("lj"); // find the path of the deleted file $. ajax ({url: "deletecl. php ", // compile the data: {lj: lj}, type:" POST ", dataType:" TEXT ", success: function (a) {window. location. href = "file_homework.php"; // refresh page }})})
(3) It is very easy to delete the page, that is, find the value and delete the file by using the delete file method.
<? Php $ lj = $ _ POST ["lj"]; unlink ($ lj); // Method for deleting an object unlink ()?>
The deletion is also completed. Check the effect. delete the file we created earlier.
The file has been deleted ~~
In addition to these basic functions, there are also many file management functions that will be updated later.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.