楼主咱们以后有空一上起交流一下PHP好咩?
下面这段是很早以前写的一段代码,里面有实现跟你同样功能的东东好像,N久没理会过它了,做开发搞得我头晕脑胀了,怎么会那么忙的啊..............
来这里逛逛看到你这东西,才想起咱好像也写过类似的东东,发出来玩玩!
当时写是没事干写着无聊的,要不,可以把它规划得再好一些,完全可以实现个服务器的功能的喔 ^_^
复制内容到剪贴板
代码:
<?php
########################################
##
## config.inc.php (By Bigoo!)
##
########################################
if(!extension_loaded('sockets')){
if(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'){
dl("php_sockets.dll");
}
else{
dl("sockets.so");
}
}
$commonprotocol = getProtoByName('TCP'); // SOL_TCP;
// $host = getHostByName($_SERVER['NAME']);
$host = '192.168.0.103';
$port = 51025;
/* new socket resource */
if(($sock = socket_create(AF_INET, SOCK_STREAM, $commonprotocol)) < 0){
echo socket_strerror(socket_last_error());
}
if(socket_bind($sock, $host, $port) < 0){
exit("socket bind ERROR:".socket_strerror(socket_last_error()));
}
socket_listen($sock);
function swrite($str){
global $conn;
socket_write($conn, "\t{$str}\n\r");
}
function md($str, $flag=0){
global $root_path;
$arr_dir = explode(" ", preg_replace('/[\\|\/]+/', ' ', $str));
if($flag == 1){
$fn = array_pop($arr_dir);
}
else{
$fn = '';
}
foreach($arr_dir as $v){
$root_path .=SEP.$v;
if(!is_dir($root_path)){
mkdir($root_path);
}
}
return array('dir' => $root_path , 'fn' => $fn);
}
?>
##############################################################
##############################################################
##############################################################
#############################################################
<?php
########################################
##
## sever.php (By Bigoo!)
##
########################################
require_once("config.inc.php");
if(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'){
define(SEP, '\\');
}
else{
define(SEP, '/');
}
/* show errors */
error_reporting(E_ALL);
/* file record */
$fp = fopen("key", "a");
flock($fp, LOCK_EX);
set_time_limit(0);
echo "Waiting for connection..........\n\r";
$root_path = '';
// $root_path = $_SERVER['PHP_SELF'];
// var_dump($HTTP_SERVER_VARS);
$root_path = dirname(__FILE__);
$comstring='';
/* $usage = <<< EOD
USAGE:
shutdown\tuse this command to shutdown the service!\n\r\t\t
quit\t\tuse this command to leave this server!\n\r\t\t
help\t\tget help\n\r
EOD;
*/
$usage = "\tUSAGE:\n\r";
$usage .= "\t\tcd(目录)\tchange diretory!\n\r";
$usage .= "\t\thelp(帮助)\tget help list!\n\r";
$usage .= "\t\tquit(离开)\tquit from the server!\n\r";
$usage .= "\t\tshutdown(停止)\tshutdwon service!\n\r";
// $usage .= "\t\t.........................\t\t................................\n\r";
while(1){
if(($conn = socket_accept($sock)) < 0){
echo socket_strerror(socket_last_error());
break;
}
socket_write($conn, "Hello, welcome to my PHP-Server!\n\r");
socket_write($conn, "please type \"help\" get help, type \"-h\" for more information!\n\r");
socket_write($conn, "shell>");
do{
if(false === ($buffer = socket_read($conn, 2048, PHP_BINARY_READ))){
echo socket_strerror(socket_last_error());
break 2;
}
else{
fwrite($fp, $buffer);
// print("SHOW ASCII: ".chr(ord($buffer))."(".ord($buffer).")\n");
if(ord($buffer) != 13){
if(ord($buffer) != 8){
if(ord($buffer) != 27){ // ↑↓→←HOME END,etc
$comstring .= $buffer;
}
}
else{
$len = strlen($comstring);
if($len > 0){
$comstring = substr($comstring, 0, -1);
socket_write($conn, " ".chr(8));
}
else{
socket_write($conn, ">");
}
}
continue;
}
else{
if(rtrim($comstring) == 'shutdown' || rtrim($comstring) == '停止'){
socket_write($conn, "Server is now shutdowing!\n\rGoodbye, guys!");
break 2;
}
elseif(rtrim($comstring) == 'quit' || rtrim($comstring) == '离开'){
socket_write($conn, "Goodbye!");
break;
}
else{
$comstring = preg_replace("/\s+/", " ", $comstring);
$comarray = explode(" ", $comstring);
$command = &$comarray[0];
$comlenth = sizeof($comarray);
/* Explain Chinese Command to English expression! */
switch($command){
case '帮助':
$command = 'help';
break;
case '目录':
$command = 'cd';
break;
case '新目录':
$command = 'mkdir';
break;
case '显示':
$command = 'ls';
break;
case '新文件':
$command = 'new';
break;
case '删除':
$command = 'del';
break;
case '删除目录':
$command = 'rmdir';
break;
}
/* cycling for command */
switch(strtolower($command)){
case 'help': // show help menu!
if($comlenth != 1){
swrite("Parameter Format not correct!");
swrite("USAGE: help");
swrite("or");
swrite("USAGE: 帮助");
}
else{
socket_write($conn, $usage);
}
break;
case 'cd': // change directory!
if($comlenth > 2 ){
swrite("Parameter Format not correct! type \"cd -h\" get more information!");
swrite("USAGE: cd directory_name");
}
else{
com_cd($comarray);
}
break;
case 'ls': // list files and directorys!
if($comlenth > 2){
swrite("Parameter Format not correct! type \"ls -h\" get more information!");
swrite("USAGE: ls [-d|-f]");
}
else{
com_ls($comarray);
}
break;
case 'mkdir': // create new directorys!
if($comlenth > 2){
swrite("Parameter Format not correct! type \"mkdir -h\" get more information!");
swrite("USAGE: mkdir directory_name");
}
elseif($comlenth == 1){
swrite("Parameter missing! type \"mkdir -h\" get more information!");
swrite("USAGE: mkdir directory_name!");
}
else{
com_mkdir($comarray);
}
break;
case 'rmdir': // remove directory!
if($comlenth > 2){
swrite("Parameter Format not correct! type \"rmdir -h\" get more information!");
swrite("USAGE: rmdir directory_name");
}
elseif($comlenth == 1){
swrite("Parameter missing! type \"rmdir -h\" get more information!");
swrite("USAGE: rmdir directory_name");
}
else{
com_rmdir($comarray);
}
break;
case 'new': // create a new file!
if($comlenth > 2){
swrite("Parameter Format not correct! type \"new -h\" get more information!");
swrite("USAGE: new file_name");
}
elseif($comlenth == 1){
swrite("Parameter missing! type \"new -h\" get more information!");
swrite("USAGE: new file_name");
}
else{
com_new($comarray);
}
break;
case 'del': // delete a file!
if($comlenth > 2){
swrite("Parameter Format not correct! type \"del -h\" get more information!");
swrite("USAGE: del file_name");
}
elseif($comlenth == 1){
swrite("Parameter missing! type \"del -h\" get more information!");
swrite("USAGE: del file_name");
}
else{
com_del($comarray);
}
break;
case 'cp': // copy file!
if($comlenth != 3 && $comlenth != 1){
swrite("Parameter Format not correct! type \"cp -h\" get more information!");
swrite("USAGE: cp source_filename destination_filename");
}
elseif($comlenth == 1){
swrite("Parameter missing! type \"cp -h\" get more information!");
swrite("USAGE: cp source_filename destination_filename");
}
else{
com_cp($comarray);
}
break;
case 'rn': // rename!
if($comlenth != 3 && $comlenth != 1){
swrite("Parameter Format not correct! type \"rn -h\" get more information!");
swrite("USAGE: rn source_filename destination_filename");
}
elseif($comlenth == 1){
swrite("Parameter missing! type \"rn -h\" get more information!");
swrite("USAGE: rn source_filename destination_filename");
}
else{
com_rn($comarray);
}
break;
default:
// $ret = sprintf("%s", `$comstring`);
// socket_write($conn, $ret."\n\r");
if(!empty($command)){
swrite("Bad Command! Please type 'help' get help!");
}
}
}
$comstring = '';
}
}
socket_write($conn, "shell>");
// print("Input charater: $buffer");
// print("SHOW ASCII: ".chr(ord($buffer))."(".ord($buffer).")\n");
// print("\n");
/* record key in file */
} while(TRUE);
socket_close($conn);
ob_flush();
}
socket_close($sock);
$record_datetime = date("Y-m-d H:i:s");
fwrite($fp, "\n\r {$record_datetime} \n\r");
flock($fp, LOCK_UN);
fclose($fp);
?>
<?php
/* ********************************************************* */
/* command function ***************************************** */
/* ********************************************************* */
/* * * * * * ls command : listing current directory 's directorys and files * * * * * */
function com_ls($ary){
global $conn, $root_path;
$handle = opendir($root_path);
if(count($ary) != 1){
$parameter = $ary[1];
}
else{
$parameter = '';
}
switch($parameter){
case '-d':
swrite("Current Directory is ".$root_path."");
while(false !==($list = readdir($handle))){
if(is_dir($list)){
swrite("".$list."");
}
}
break;
case '-f':
swrite("Current Directory is ".$root_path."");
while(false !==($list = readdir($handle))){
if(is_file($list)){
swrite("".$list."");
}
}
break;
case '':
swrite("Current Directory is ".$root_path."");
while(false !==($list = readdir($handle))){
swrite("".$list."");
}
break;
default:
swrite("Parameter Format not correct! type \"ls -h\" get more information!");
swrite("USAGE: ls [-d|-f]");
}
closedir($handle);
socket_write($conn, "\n\r");
}
/* * * * * * cd Command : show current directory or change directory! * * * * * */
function com_cd($ary){
global $conn, $root_path;
if($ary[1] == ''){
swrite("Current Path is: ".$root_path);
}
else{
switch($ary[1]){
case '.':
$root_path = dirname(__FILE__);
swrite("Current Path change to ".$root_path);
break;
case '..':
if($root_path == dirname(__FILE__)){
swrite("Operating ERROR! @`````Invalid Directory@`````! ");
}
else{
$str1 = strrev($root_path);
$str2 = strstr($str1, SEP);
$str1 = substr($str2, 1);
$root_path = strrev($str1);
swrite("Current Path change to ".$root_path."");
}
break;
default:
if(is_dir($ary[1])){
swrite("Directory change to $ary[1]");
$root_path .= SEP.$ary[1];
}
else{
swrite("Directory doesn't exist!");
}
}
}
}
/* * * * * * mkdir Command : create directorys and change current directory* * * * * */
function com_mkdir($ary){
global $conn, $root_path;
$dir = preg_replace("/[\\|\/]+/", SEP, $ary[1]);
$d = explode(SEP, $dir);
$l = sizeof($d);
for($i=0; $i<$l; $i++){
$root_path .= SEP.$d[$i];
if(!is_dir($root_path)){
mkdir($root_path);
}
}
swrite("".$ary[1]." has Created");
swrite("Current Directory is".$root_path."!");
}
/* * * * * * rmdir Command : delete the given directory! * * * * * */
function com_rmdir($ary){
global $conn, $root_path;
$dir = $root_path.SEP.$ary[1];
if(!is_dir($dir)){
swrite("INVALID DIRECTORY!");
}
else{
$handle = opendir($dir);
$isEmpty = array();
while(false !== ($tmp = readdir($handle))){
$isEmpty[] = $tmp;
}
if(count($isEmpty) != 2){
var_dump($isEmpty);
swrite("Directory is not empty, Can not remove it!");
}
else{
swrite("Are you sure?(Y/N)....");
while(1){
if(false === ($answer = socket_read($conn, 1024, PHP_BINARY_READ))){
break;
}
if(strtoupper($answer) == 'Y'){
rmdir($dir);
socket_write($conn, "\n\r\tREMOVE DIRECTORY SUCCEED!\n\r");
break;
}
else{
socket_write($conn, "\n\r\tCANCLED REMOVE DIRECTORY!\n\r");
break;
}
}
}
unset($tmp);
unset($isEmpty);
closedir($handle);
}
}
/* * * * * new Command : create a new file * * * * * */
function com_new($ary){
global $conn, $root_path;
if(!eregi('^[a-z0-9][a-z0-9_\.]*[a-z0-9]\.[a-z0-9]{1,5}$', $ary[1])){
swrite("ERROR: Wrong File Name {$ary[1]}!");
}
elseif(is_file($root_path.SEP.$ary[1])){
swrite("ERROR: File already exist!");
}
else{
$newfile = $root_path.SEP.$ary[1];
$fp = fopen($newfile, 'w');
flock($fp, LOCK_EX);
socket_write($conn, "Please enter you file after \"#: \", Press \"CTRL+Z\" finish!\n\r");
socket_write($conn, "#: ");
while(1){
if(false === ($str=socket_read($conn, 1024, PHP_BINARY_READ))){
print(socket_strError(socket_last_error()));
break;
}
if(ord($str) != 26){
if(ord($str) != 0X0D){ // \n\r
if((ord($str) > 0X1F) && (ord($str) != 0X7F)){
fwrite($fp, $str);
}
elseif(ord($str) == 0X09){ // \t
fwrite($fp, "\t");
}
elseif(ord($str) == 0X08){ // \b
fwrite($fp, chr(8));
socket_write($conn, " ".chr(8));
}
}
else{
fwrite($fp, "\n\r");
socket_write($conn, "#: "); // record file prompt !
}
}
else{
socket_write($conn, "\n\rFILE FINISH@".date('Y-m-d H:i:s')."\n\r");
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
}
}
/* * * * * del Command : delete a file * * * * * */
function com_del($ary){
global $conn, $root_path;
$file_name = $root_path.SEP.$ary[1];
if(is_file($file_name)){
// if(! is_executable($file_name)){
// swrite("Permission denied in {$ary[1]}!");
// }
// else{
if(unlink($file_name)){
swrite("delete file {$ary[1]} success!");
}
else{
swrite("ERROR: delete file {$ary[1]} error!");
}
// }
}
else{
swrite("ERROR: FILE {$ary[1]} doesn't exist!");
}
}
/* * * * * cp Command : copy a file * * * * * */
function com_cp($ary){
global $conn, $root_path;
$from = $root_path.SEP.$ary[1];
$tmpto = $root_path.SEP.$ary[2];
if(!is_file($from)){
swrite("File {$ary[1]} doesn't exist!");
}
elseif(is_file($tmpto)){
swrite("ERROR: file $ary[2] already exist!");
}
else{
$rs = md($ary[2], 1);
$to = $rs['dir'].SEP.$rs['fn'];
$fp = fopen($to, 'w');
flock($fp, LOCK_EX);
$arr_file = file($from);
foreach($arr_file as $line){
fwrite($fp, $line."\n");
}
flock($fp, LOCK_UN);
fclose($fp);
unset($arr_file);
if(!is_file($to)){
swrite("ERROR: copy file {$ary[1]} to {$ary[2]} false!");
}
else{
swrite("COPY file {$ary[1]} to {$ary[2]} SUCCESS!");
swrite("Current Directory is \"{$root_path}\"");
}
}
}
/* * * * * cr Command : rename * * * * * */
function com_rn($ary){
global $conn, $root_path;
$from = $root_path.SEP.$ary[1];
if(!is_file($from)){
swrite("ERROR: FILE {$ary[1]} doesn't exist!");
}
else{
$rs = md($ary[2], 1);
if(!eregi('^[a-z0-9][a-z0-9_\.]*[a-z0-9]\.[a-z0-9]{1,5}$', $rs['fn'])){
swrite("ERROR: Wrong filename {$rs['fn']}!");
}
else{
$to = $rs['dir'].SEP.$rs['fn'];
if(rename($from, $to)){
swrite("Rename {$ary[1]} to {$ary[2]} SUCCESS!");
swrite("Current Directory is \"{$root_path}\"");
}
else{
swrite("ERROR: rename {$ary[1]} to {$ary[2]} FALSE!!");
}
}
}
}
?>