Online website file management

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn simple online file system management.
File traversal. I haven't finished writing it yet.
OnlineEditor. php file code
// Online file editor
/*-------------------------------------
Implemented using the factory design pattern and MVC
*/

Class onlineEditor {

// Set the global variable path
Public $ filePath = null;
// Set the filtering information
Private $ fileFilter = array (
'Onlineeditor. php ',
'ViewEditor.html ',
'Index. php ',
'.',
'..'
);

// The constructor must be private in the singleton design mode.
Function _ construct ($ filePath ){
$ This-> filePath = $ filePath;
}

// Operations performed when the class is destroyed
Function _ destruct (){
// Echo $ this-> filePath;
}

// Obtain the object content
Function getContent ($ filePath ){
If (! Isset ($ filePath )){

} Else {
// Obtain the File Content
$ FileContent = file_get_contents ($ filePath );
Return $ fileContent;
}
}

// Put the file content
Function putContent ($ filePath, $ fileContent ){
File_put_contents ($ filePath, $ fileContent );
}

// Determine whether the directory exists
Private function judgeExist (){
// Determine whether the directory is empty or no file
If (is_dir ($ this-> filePath) & file_exists ($ this-> filePath )){
Return true;
} Else {
Return false;
}
}

// Create a file
Function createFile ($ filename ){
If (! File_exists ($ filename )){
Fopen ($ filename, "w + ");
}

Else {
Echo "Click here to return ";
Die ("the file already exists ");
}

}
// Delete an object
Function delFile ($ filename ){
If (file_exists ($ filename )){
Unlink ($ filename );
}
}

// Main Function
Function main (){
If ($ this-> judgeExist ()){
// Obtain the object that opens the folder
$ FileOpen = opendir ($ this-> filePath );
$ FileMes = array ();
$ I = 0;
// Traverse folders
While ($ file = readdir ($ fileOpen )){

// Filter
If (in_array ($ file, $ this-> fileFilter )){
Continue;
}

$ FileMesPush = array (
'Filecode' => $ I,
'Filename' => $ file,
'Filetype' => fileType ($ file ),
'Filesize' => fileSize ($ file ),
'Filemtime' => filemtime ($ file)
);

Array_push ($ fileMes, $ fileMesPush );
$ I ++;
}
// Close the file

Return $ fileMes;
Fclose ($ fileOpen );
} Else {
Die ("this folder does not exist ");
}
}

}

?>
Index. php

$ DirPath = "./"; // you can change the operation directory to your preferred one.
$ Action = null;

// Import the file
Require "./onlineEditor. php ";

// Obtain the onlineEditor object
$ OnlineEditor = new onlineEditor ($ dirPath );
$ FileMes = $ onlineEditor-> main ();

// Process the file path
Function subFilePath ($ dirPath, $ filename ){
// Echo $ dirPath. $ filename;
Return $ dirPath. $ filename;
}

// Initialization
If (array_key_exists ('action', $ _ GET )){
Switch ($ _ GET ['action']) {
Case 'updata ':
$ Action = 'updata ';
Break;
Case 'del ':
$ OnlineEditor-> delFile (subFilePath ($ dirPath, $ _ GET ['filename']);
$ Action = 'del ';
Echo subFilePath ($ dirPath, $ _ GET ['filename']);
Echo "script location. href = 'index. php'; script";
Break;
}
} Else {
$ Action = null;
}

If (array_key_exists ('action', $ _ POST )){
Switch ($ _ POST ['action']) {
Case 'create ':
$ OnlineEditor-> createFile (subFilePath ($ dirPath, $ _ POST ['filename']);
Echo "script location. href = 'index. php'; script";
Break;
}
}

// Obtain the File Content
If (array_key_exists ('filename', $ _ GET) & $ _ GET ['action'] = 'updata '){
$ Root = subFilePath ($ dirPath, $ _ GET ['filename']);
$ FileContent = $ onlineEditor-> getContent ($ root );
} Else
$ FileContent = "persistence is victory ";

If (array_key_exists ('filecontent', $ _ POST )){
// Var_dump ($ _ POST );
$ OnlineEditor-> putContent (subFilePath ($ dirPath, $ _ POST ['filename']), $ _ POST ['filecontent']);
Echo "script location. href = 'index. php'; script";
}


// Introduce the interface
Require "./viewEditor.html ";

// Print_r ($ fileMes );



?>
ViewEditor. php


Online file editor

Related Article

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.