ajax+php File Manager

Source: Internet
Author: User
Tags create directory explode

Found in the previous two years written a file manager, feel very useful, share out, a total of two files file.html,file.php upload to the directory, upload function please download the uploadify and then configure the SWF address in file.html

File.html

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 



file.php

<?php/** * File Operation section * TONGJH original, communication please email to [email protected]*///file upload if ($_post["ename"] = = ' uploads ') {$path = '/'. $_ post[' path ']. ' /';//upload path $verifytoken = MD5 (' Unique_salt '. $_post[' timestamp ']);//security detection, directory closed if (!empty ($_files)) {$tempFile = $_ files[' Filedata ' [' tmp_name '];//uploaded file name $targetpath = $_server[' Document_root ']. $path;//The path of the uploaded file $targetfile = RTrim ($targetPath, '/'). ‘/‘ . $_files[' Filedata ' [' name '];//uploaded file location $filetypes = array (' jpg ', ' jpeg ', ' gif ', ' PNG '); allowable upload file type $fileparts = PathInfo ($_files[' Filedata ' [' name ']);//Upload file type if (In_array ($fileParts [' extension '],$ FileTypes) {move_uploaded_file ($tempFile, $targetFile); Echo ' upload file '. $targetFile. ' Success ';} else {echo ' uploaded file type is not allowed ';}}} Add the directory if ($_get["ename"] = = ' Adddir ') {if (file_exists ($_get[' Edir '). ' /'. $_get[' fname ')) {$msg = array (' ERROR ' = ' directory already exists ');} Else{if (@mkdir ($_get[' Edir '). ' /'. $_get[' fname ')) {$msg = array (' OK ' = ' = ' successfully established directory '. $_get[' fname '), ' fname ' =>$_get[' Edir ']. ' /'. $_get[' fname ');} else{$msg = Array (' ERROR ' = ' cannot create directory ');}}echo Json_encode ($msg);} Rename the directory if ($_get["ename"] = = ' Rename ') {$rename = explode ('/', $_get[' Edir ']), for ($i =0; $i <count ($rename)-1; $i + +) {$ str. = $rename [$i]. ' /‘;} $rename = $str. $_get[' fname '];if (@rename ($_get[' Edir '], $rename)) {$msg = array (' OK ' \ = ' successfully renamed directory '. $rename, ' fname ' = > $rename);} else{$msg = Array (' ERROR ' \ = ' cannot rename directory ');} echo Json_encode ($msg);} Delete directory if ($_get["ename"] = = ' Deledir ') {if (@rmdir ($_get[' Edir ')) {$msg = array (' OK ' and ' \ = ' successfully delete directory '. $_get[' Edir ']);} else{$msg = Array (' ERROR '/' = ' cannot delete directory, may not be an empty directory or does not have this directory '. $_get[' Edir ');} echo Json_encode ($msg);} Open Directory if ($_get[' path ']) {$path = $_get[' path '];if ($path = = ' root ') {$path = $_server[' Document_root '];} $files = GetFiles ($path); Echo Json_encode ($files);} Add the file if ($_get["ename"] = = ' AddFile ') {if (@touch ($_get[' Edir '). ' /'. $_get[' fname ')) {$msg = array (' OK ' = ' = ' successfully add file '. $_get[' fname '), ' fname ' =>$_get[' Edir ']);} else{$msg = Array (' ERROR ' = ' cannot build file ');} echo Json_encode ($msg);} Rename file if ($_get["ename"] = = ' RenameFile ') {$rename = explode ('/', $_get[' Edir ']); for ($i =0; $i <count ($rename)-1; $i + +) {$str. = $rename [$i]. ' /‘;} $rename = $str. $_get[' fname '];//new file name if (@rename ($_get[' Edir '], $rename)) {$msg = array (' OK ' and ' = ') ' successfully renamed file '. $rename, ' FName ' =>substr ($str, 0,-1));} else{$msg = Array (' ERROR ' \ = ' cannot rename file '. $rename);} echo Json_encode ($msg);} Edit file if ($_get["ename"] = = ' Editfile ') {/* Determine file type */$finfo = pathinfo ($_get[' url '); $filetype = $finfo ["extension"];$ Picarray = array (' jpg ', ' jpeg ', ' gif ', ' png ', ' ico '); $txtArray = Array (' php ', ' txt ', ' inc ', ' HTML ', ' htm ', ' js ', ' CSS ', ' ASP ') if (In_array ($filetype, $picArray)) {//file is picture $msg["filestr"] = $_get[' url '), $msg ["filetype"] = "pic";} ElseIf (In_array ($filetype, $txtArray)) {//file is text type $msg["filestr"] = GbkToUtf8 (@file_get_contents ($_get["url"]); $ msg["FileType"] = "txt";} else{//file is not editable $msg["filestr"] = "file is not editable";} echo Json_encode ($msg);} Save the file if ($_get["ename"] = = ' FileSave ') {if (@file_put_contents ($_get["Edir"],stripslashes ($_get["fname"])) {$msg [" OK "] =" successfully modified file ";} else{$msg ["error"] = "File not Found";} Echo json_encodE ($msg);} Delete file if ($_get["ename"] = = ' Delefile ') {if (@unlink ($_get[' Edir ')) {$msg = array (' OK ' and ' = ' = ' successfully delete file '. $_get[' Edir ']);} else{$msg = Array (' ERROR ' = = ' cannot delete file, please check if you have delete permission '. $_get[' Edir ');} echo Json_encode ($msg);} /** * Get all directories and files under a directory * @access public * @param string $path given directory * @return Array (three-dimensional array) If you successfully return all directories and files under a directory,  Failure returns an array of failure hints */function getFiles ($path) {if ($fh = @opendir ($path)) {while (false!== ($file = Readdir ($fh))) {if ($file = = '. ') || $file = = ' ... ') {continue;} $filetype = filetype ($path. ' /'. $file), if ($filetype = = ' dir ') {$files [' dirs '] [$file] [' dir '] = $file; $files [' dirs '] [$file] [' path '] = $path. ' /'. $file;} if ($filetype = = ' file ') {$files [' files '] [$file] [' file '] = $file; $files [' Files '] [$file] [' path '] = $path. ' /'. $file;}} if (Is_array ($files)) {return $files;} Else{return Array (' ERROR ' = ' no file ');}} Else{return Array (' ERROR ' = ' no directory ');}} /** * String encoding conversion, converted to Uft8 */function GbkToUtf8 ($value) {$code = Chkcode ($value), if ($code = = "GBK") {return Iconv ("GBK", "utf- 8 ", $value);} IF ($code = = "GB2312") {return Iconv ("GB2312", "UTF-8", $value);} if ($code = = "GB18030") {return Iconv ("GB18030", "UTF-8", $value);} return $value;} /** * Gets the encoding of the string */function Chkcode ($string) {$code = array (' UTF-8 ', ' GBK ', ' GB18030 ', ' GB2312 '); foreach ($code as $c) {if ($s Tring = = = Iconv (' UTF-8 ', $c, Iconv ($c, ' UTF-8 ', $string))} {return $c;}}? >



ajax+php File Manager

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.