📄 fileman.php
字号:
if(@ftp_rmdir($this->ftpconn,$deldir))
$this->debug("delete dir::".$deldir);
else
$this->SetError("Error of delete dir::".$deldir);
if($deldir) $deldirs++;
}
if($this->action=='getfiles'){
$curindex= $info[$level]["index"];
$deldir= $info[$level][$curindex-1];
$this->files[$i]['name']=$deldir;
$this->files[$i]['folder']=1;
}
//echo "--->level:".$level."<br>";
} //end if
//echo "tempflag:".$tempflag."<br>";
}//end while
}//end if
}//end while
$dirnums++;
$this->debug( "文件夹: $this->dirnums 个;文件: $this->filenums 个,size:$this->totalsize bytes");
if($this->action=='view') return $list;
}//end func
function fileext($file)
{
$p = pathinfo($file);
return strtolower($p['extension']);
}
function convertsize($size){
$times = 0;
$comma = '.';
while ($size>1024){
$times++;
$size = $size/1024;
}
$size2 = floor($size);
$rest = $size - $size2;
$rest = $rest * 100;
$decimal = floor($rest);
$addsize = $decimal;
if ($decimal<10) {$addsize .= '0';};
if ($times == 0){$addsize=$size2;} else
{$addsize=$size2.$comma.substr($addsize,0,2);}
switch ($times) {
case 0 : $mega = ' bytes'; break;
case 1 : $mega = ' KB'; break;
case 2 : $mega = ' MB'; break;
case 3 : $mega = ' GB'; break;
case 4 : $mega = ' TB'; break;}
$addsize .= $mega;
return $addsize;
}
function mytempname($dir, $prefix, $postfix) {
if ($dir[strlen($dir) - 1] == '/') { $trailing_slash = ""; }
else { $trailing_slash = "/"; }
// Check if the $dir is a directory
if (!is_dir(realpath($dir)) || filetype(realpath($dir)) != "dir") { return false; }
// Check if the directory is writeable
if (!is_writable($dir)){ return false; }
// Create the temporary filename
do {
$seed = substr(md5(microtime()), 0, 8);
$filename = $dir . $trailing_slash . $prefix . $seed . $postfix;
} while (file_exists($filename));
$filename = $trailing_slash . $prefix . $seed . $postfix;
return $filename;
} // end mytempnam
function LoginFtp($ftp,$user,$pass,$initdir)
{
$this->ftpconn = @ftp_connect($ftp);
$login_result = @ftp_login($this->ftpconn, $user, $pass);
if ((!$this->ftpconn) || (!$login_result))
{
$this->SetError("Ftp::can't login ftp $ftp with $user");
@ftp_close($this->ftpconn);
return false;
}
else
{
$this->debug("Ftp::login ftp $ftp with $user");
}
if($initdir=="") $$initdir="/";
if(!@ftp_chdir($this->ftpconn,$initdir))
{
//ftp_chdir($conn_id,"/");
$this->SetError("Ftp::Can't change to directory $initdir,change to / instead!");
ftp_close($this->ftpconn);
return false;
}
else
{
$this->debug("Ftp::change to directory $initdir");
ftp_chdir($this->ftpconn,'/');//reset
}
return 1;
}
function ftpAsciiBinary($filename) {
// --------------
// Checks the first character of a file and its extension to see if it should be
// transferred in ASCII or Binary mode
//
// Default: FTP_BINARY
// Exceptions: FTP_ASCII (files which start with a dot, and a list of exceptions)
// A file with more than 1 dot: the last extension is taken into account
//
// --------------
// -------------------------------------------------------------------------
// If the first character is a dot, return FTP_ASCII
// -------------------------------------------------------------------------
$firstcharacter = substr($filename, 0, 1);
if ($firstcharacter == ".") {
$ftpmode = FTP_ASCII;
return $ftpmode;
}
// -------------------------------------------------------------------------
// If the first character is not a dot, check the extension
// -------------------------------------------------------------------------
//$last = get_filename_extension($filename);
$last=strtolower(strrchr($filename,'.'));
$last=substr($last,1);
if (
$last == "asp" ||
$last == "bas" ||
$last == "bat" ||
$last == "c" ||
$last == "cfg" ||
$last == "cfm" ||
$last == "cgi" ||
$last == "conf" ||
$last == "cpp" ||
$last == "css" ||
$last == "dhtml" ||
$last == "diz" ||
$last == "default" ||
$last == "file" ||
$last == "h" ||
$last == "hpp" ||
$last == "htaccess" ||
$last == "htpasswd" ||
$last == "htm" ||
$last == "html" ||
$last == "inc" ||
$last == "ini" ||
$last == "js" ||
$last == "jsp" ||
$last == "mak" ||
$last == "msg" ||
$last == "nfo" ||
$last == "old" ||
$last == "pas" ||
$last == "patch" ||
$last == "perl" ||
$last == "php" ||
$last == "php3" ||
$last == "phps" ||
$last == "phtml" ||
$last == "pinerc" ||
$last == "pl" ||
$last == "pm" ||
$last == "qmail" ||
$last == "readme" ||
$last == "setup" ||
$last == "sh" ||
$last == "shtml" ||
$last == "sql" ||
$last == "style" ||
$last == "tcl" ||
$last == "tex" ||
$last == "threads" ||
$last == "tmpl" ||
$last == "tpl" ||
$last == "txt" ||
$last == "ubb" ||
$last == "vbs" ||
$last == "xml" ||
$last == "conf" ||
strstr($last, "htm")
) { $ftpmode = FTP_ASCII; }
else { $ftpmode = FTP_BINARY; }
//echo "<font color=green>".$filename. ",$last,$ftpmode</font>";
return $ftpmode;
} // end ftpAsciiBinary
function ftp_getlist($conn_id, $directory) {
// -------------------------------------------------------------------------
// Replace \' by \\' to be able to delete directories with names containing \'
// -------------------------------------------------------------------------
if (strlen($directory) > 1) { $directory1 = str_replace("\'", "\\\'", $directory); }
else { $directory1 = $directory; }
// -------------------------------------------------------------------------
// Step 1 - Chdir to the directory
// This is to check if the directory exists, but also because otherwise
// the ftp_rawlist does not work on some FTP servers.
// -------------------------------------------------------------------------
$result1 = @ftp_chdir($conn_id, $directory1);
// -------------------------------------------------------------------------
// Step 2 - Get list of directories and files
// The -a option is used to show the hidden files as well on some FTP servers
// Some servers do not return anything when using -a, so in that case try again without the -a option
// -------------------------------------------------------------------------
$rawlist = ftp_rawlist($conn_id, "-a");
if (sizeof($rawlist) <= 1) { $rawlist = ftp_rawlist($conn_id, ""); }
// -------------------------------------------------------------------------
// Step 3 - Parse the raw list to get an array
// -------------------------------------------------------------------------
for($i=0; $i<count($rawlist); $i++) {
$templist[$i] = $this->ftp_scanline($rawlist[$i]);
} // End for
// -------------------------------------------------------------------------
// Step 4 - Move the rows so that
// 1. the array would contain elements from 1 to n
// 2. the list would be sorted directories first, then files, then symlinks, then unrecognized
// -------------------------------------------------------------------------
$i = 0; // $i is the index of templist and could go from 0 to n+3
$j = 1; // $j is the index of list and should go from 1 to n (n being the nr of valid rows)
$list_directories = array();
$list_files = array();
$list_symlinks = array();
$list_unrecognized = array();
for ($i=0; $i<count($templist); $i=$i+1) {
if (is_array($templist[$i]) == true) {
if ($templist[$i]['dirorfile'] == "d") { array_push($list_directories, $templist[$i]); }
elseif ($templist[$i]['dirorfile'] == "-") { array_push($list_files, $templist[$i]); }
elseif ($templist[$i]['dirorfile'] == "l") { array_push($list_symlinks, $templist[$i]); }
elseif ($templist[$i]['dirorfile'] == "u") { array_push($list_unrecognized, $templist[$i]); }
}
}
for ($i=0; $i<count($list_directories); $i=$i+1) { $list[$j] = $list_directories[$i]; $j=$j+1; }
for ($i=0; $i<count($list_files); $i=$i+1) { $list[$j] = $list_files[$i];$j=$j+1; }
//for ($i=0; $i<count($list_symlinks); $i=$i+1) { $list[$j] = $list_symlinks[$i]; $j=$j+1; }
//for ($i=0; $i<count($list_unrecognized); $i=$i+1) { $list[$j] = $list_unrecognized[$i]; $j=$j+1; }
// -------------------------------------------------------------------------
// Step 5 - Return the result
// -------------------------------------------------------------------------
$list_warnings_directory[1] = $list;
$list_warnings_directory[2] = $warnings;
$list_warnings_directory[3] = $directory;
//print_r($list);
$result1 = @ftp_chdir($conn_id, '/');
return $list;
} // End function ftp_getlist
function ftp_scanline($rawlistline) {
// --------------
// This function scans an ftp_rawlist line string and returns its parts (directory/file, name, size,...) using ereg()
//
// !!! Documentation about ereg and FTP server's outputs are now at the end of the function !!!
// --------------
// -------------------------------------------------------------------------
// Scanning:
// 1. first scan with strict rules
// 2. if that does not match, scan with less strict rules
// 3. if that does not match, scan with rules for specific FTP servers (AS400)
// 4. and if that does not match, return the raw line
// -------------------------------------------------------------------------
// ----------------------------------------------
// 1. Strict rules
// ----------------------------------------------
if (ereg("([-dl])([rwxst-]{9})[ ]+([0-9]+)[ ]+([^ ]+)[ ]+(.+)[ ]+([0-9]+)[ ]+([a-zA-Z]+[ ]+[0-9]+)[ ]+([0-9:]+)[ ]+(.*)", $rawlistline, $regs) == true) {
// permissions number owner group size month day year/hour filename
$listline['scanrule'] = "rule-1";
$listline['dirorfile'] = "$regs[1]"; // Directory ==> d, File ==> -
$listline['dirfilename'] = "$regs[9]"; // Filename
$listline['size'] = "$regs[6]"; // Size
$listline['owner'] = "$regs[4]"; // Owner
$listline['group'] = "$regs[5]"; // Group
$listline['permissions'] = "$regs[2]"; // Permissions
$listline['mtime'] = "$regs[7] $regs[8]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
}
// ----------------------------------------------
// 2. Less strict rules
// ----------------------------------------------
elseif (ereg("([-dl])([rwxst-]{9})[ ]+(.*)[ ]+([a-zA-Z0-9 ]+)[ ]+([0-9:]+)[ ]+(.*)", $rawlistline, $regs) == true) {
// permissions number/owner/group/size
// month-day year/hour filename
$listline['scanrule'] = "rule-2";
$listline['dirorfile'] = "$regs[1]"; // Directory ==> d, File ==> -
$listline['dirfilename'] = "$regs[6]"; // Filename
$listline['size'] = "$regs[3]"; // Number/Owner/Group/Size
$listline['permissions'] = "$regs[2]"; // Permissions
$listline['mtime'] = "$regs[4] $regs[5]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
}
// ----------------------------------------------
// 3. Specific FTP server rules
// ----------------------------------------------
// ---------------
// 3.1 Windows
// ---------------
elseif (ereg("([0-9/-]+)[ ]+([0-9:AMP]+)[ ]+([0-9]*)[ ]+(.*)", $rawlistline, $regs) == true) {
// date time size filename
$listline['scanrule'] = "rule-3.1";
$listline['size'] = "$regs[3]"; // Size
$listline['dirfilename'] = "$regs[4]"; // Filename
$listline['owner'] = ""; // Owner
$listline['group'] = ""; // Group
$listline['permissions'] = ""; // Permissions
$listline['mtime'] = "$regs[1] $regs[2]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
if ($listline['size'] != "") { $listline['dirorfile'] = "-"; }
else { $listline['dirorfile'] = "d"; }
}
// ---------------
// 3.2 Netware
// Thanks to Danny!
// ---------------
elseif (ereg("([-]|[d])[ ]+(.{10})[ ]+([^ ]+)[ ]+([0-9]*)[ ]+([a-zA-Z]*[ ]+[0-9]*)[ ]+([0-9:]*)[ ]+(.*)", $rawlistline, $regs) == true) {
// dir/file perms owner size month day hour filename
$listline['scanrule'] = "rule-3.2";
$listline['dirorfile'] = "$regs[1]"; // Directory ==> d, File ==> -
$listline['dirfilename'] = "$regs[7]"; // Filename
$listline['size'] = "$regs[4]"; // Size
$listline['owner'] = "$regs[3]"; // Owner
$listline['group'] = ""; // Group
$listline['permissions'] = "$regs[2]"; // Permissions
$listline['mtime'] = "$regs[5] $regs6"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
}
// ---------------
// 3.3 AS400
// ---------------
elseif (ereg("([a-zA-Z0-9_-]+)[ ]+([0-9]+)[ ]+([0-9/-]+)[ ]+([0-9:]+)[ ]+([a-zA-Z0-9_ -\*]+)[ /]+([^/]+)", $rawlistline, $regs) == true) {
// owner size date time type filename
if ($regs[5] != "*STMF") { $directory_or_file = "d"; }
elseif ($regs[5] == "*STMF") { $directory_or_file = "-"; }
$listline['scanrule'] = "rule-3.3";
$listline['dirorfile'] = "$directory_or_file";// Directory ==> d, File ==> -
$listline['dirfilename'] = "$regs[6]"; // Filename
$listline['size'] = "$regs[2]"; // Size
$listline['owner'] = "$regs[1]"; // Owner
$listline['group'] = ""; // Group
$listline['permissions'] = ""; // Permissions
$listline['mtime'] = "$regs[3] $regs[4]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
}
// ---------------
// 3.4 Titan
// Owner, group are modified compared to rule 1
// TO DO: integrate this rule in rule 1 itself
// ---------------
elseif (ereg("([-dl])([rwxst-]{9})[ ]+([0-9]+)[ ]+([a-zA-Z0-9]+)[ ]+([a-zA-Z0-9]+)[ ]+([0-9]+)[ ]+([a-zA-Z]+[ ]+[0-9]+)[ ]+([0-9:]+)[ ](.*)", $rawlistline, $regs) == true) {
// dir/file permissions number owner group size month date time file
$listline['scanrule'] = "rule-3.4";
$listline['dirorfile'] = "$regs[1]"; // Directory ==> d, File ==> -
$listline['dirfilename'] = "$regs[9]"; // Filename
$listline['size'] = "$regs[6]"; // Size
$listline['owner'] = "$regs[4]"; // Owner
$listline['group'] = "$regs[5]"; // Group
$listline['permissions'] = "$regs[2]"; // Permissions
$listline['mtime'] = "$regs[7] $regs[8]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
}
// ----------------------------------------------
// 4. If nothing matchs, return the raw line
// ----------------------------------------------
else {
$listline['scanrule'] = "rule-4";
$listline['dirorfile'] = "u";
$listline['dirfilename'] = $rawlistline;
}
// -------------------------------------------------------------------------
// Remove the . and .. entries
// Remove the total line that some servers return
// -------------------------------------------------------------------------
if ($listline['dirfilename'] == "." || $listline['dirfilename'] == "..") { return ""; }
elseif (substr($rawlistline,0,5) == "total") { return ""; }
// -------------------------------------------------------------------------
// And finally... return the nice list!
// -------------------------------------------------------------------------
return $listline;
} // End function ftp_scanline
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -