13c05-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 237 行

PHP
237
字号
<?php// Basic FTP client software.// Start by preparing to read from stdin.$stdin = fopen('php://stdin', 'r');$server = false;$mode = FTP_ASCII;// Declare all currently valid commands:$commands = array('open', 'close', 'quit', 'exit', 'cd', 'ls', 'dir',     'pwd', 'get', 'put', 'ascii', 'binary', 'bin', 'delete', 'del');// If parameters were provided, assume they are host, user, passif ($argc > 1) {    // Remove the filename from the line    array_shift($argv);    // Now call the open function to handle this:    _open($argv);}    // Now, Sit and loop forever, taking user input and handling it.while (true) {    // First of all, give the prompt:    echo 'phpftp> ';        // Now accept User Input    $line = trim(fgets($stdin));        // Only if the line contained anything, continue:    if ($line) {        // Split the line into an array of space separated entries:        $opts = explode(' ', $line);        $cmd = strtolower(array_shift($opts));        // Take the first entry as a command, and execute the appropriate        //  function, but first make sure it is a valid command!        if (!in_array($cmd, $commands)) {            echo "! Command not supported.\n";        }        // If server is false, only allow 'open'        // Otherwise, don't allow open!        elseif (($server xor ($cmd != 'open')) && ($cmd != 'quit')                 && ($cmd != 'exit')) {            echo "! Invalid server status for this command.\n";        } else {            // Otherwise, we can execute whatever command they gave us.            // Just call it as a function name, it will exist at this point!            $func = "_{$cmd}";            $func($opts);        }    }}// Now declare a function for each command!// To open a connection:function _open($opts) {    global $server, $stdin;        // Prepare to open the connection, prompt for missing items such as host:    if (!isset($opts[0])) {        echo '? Host: ';        $opts[0] = trim(fgets($stdin));    }        // And Username:    if (!isset($opts[1])) {        echo '? User: ';        $opts[1] = trim(fgets($stdin));    }    // And Finally, password -- Note that this will be visible on the screen    if (!isset($opts[2])) {        echo '? Pass: ';        $opts[2] = trim(fgets($stdin));    }    // Now we have what we need, attempt to open!    if (!($server = @ftp_connect($opts[0]))) {        echo "! Error, cannot connect to host!\n";        $server = false;    } else {        // We connected, try to login        if (!(@ftp_login($server, $opts[1], $opts[2]))) {            echo "! Error, Username/Password not accepted!\n";            ftp_close($server);            $server = false;        } else {            // It worked, let 'em know!            echo "- Connected to {$opts[0]}\n";                        // Try to let them know the type of server as well:            if ($type = ftp_systype($server)) {                echo "- Server type: {$type}\n";            }                        // Set the mode to Ascii & go ahead and let them know             //  what directory they are in:            _ascii(0);            _pwd(0);        }    }}// To close a connection:function _close($opts) {    @ftp_close($GLOBALS['server']);    $GLOBALS['server'] = false;    echo "- Connection Closed.\n";}// To change directoriesfunction _cd($opts) {    // Try to change to the directory that they specified:    if (!(@ftp_chdir($GLOBALS['server'], @$opts[0]))) {        echo "! Error, Failed to change directory\n";    }        // In either case, give them their current directory:    _pwd(0);}// To quit the program!function _exit($opts) {    // If we are currently connected, close first:    if ($GLOBALS['server']) {        _close(0);    }    echo "- Goodbye!\n";    exit();}function _quit($opts) {    _exit($opts);}// To get a directory listing:function _ls($opts) {    // Use rawlist to get a nicely formatted version from the server.    // Pass the opts all collapsed into one string if the person used them:    $optstring = implode(' ', $opts);    if ($res = ftp_rawlist($GLOBALS['server'], $optstring)) {        foreach ($res as $r) { echo "{$r}\n"; }    } else {        // Give an error statement        echo "! Error, could not generate directory listing\n";    }}function _dir($opts) {    _ls($opts);}// To figure out what directory you are in:function _pwd($opts) {    // Get the current directory, and echo it:    $cwd = ftp_pwd($GLOBALS['server']);    echo "- Current directory: {$cwd}\n";}// To get a file from the remote host, and save it locally.function _get($opts) {    // If we don't have any options, give an error    if (!($opts)) {        echo "! Error, no file specified\n";    } else {        // If we don't have a second option, assume a desire to        // save the file into the current directory, with the same name        if (!isset($opts[1])) {            $opts[1] = basename($opts[0]);        }                // Now, attempt to save the file.        if (!@ftp_get($GLOBALS['server'], $opts[1], $opts[0],                 $GLOBALS['mode'])) {            echo "! Error - Could not download file\n";        } else {            echo "- Data saved to file: {$opts[1]}\n";        }    }}// To put a local file to the remote host:function _put($opts) {    // If we don't have any options, give an error    if (!($opts)) {        echo "! Error, no file specified\n";    } else {        // If we don't have a second option, assume a desire to        // save the file into the current directory, with the same name        if (!isset($opts[1])) {            $opts[1] = basename($opts[0]);        }                // Now, attempt to save the file.        if (!@ftp_put($GLOBALS['server'], $opts[1], $opts[0],                 $GLOBALS['mode'])) {            echo "! Error - Could not upload file\n";        } else {            echo "- Data uploaded to file: {$opts[1]}\n";        }    }}// To Change the transfer mode to ASCIIfunction _ascii($opts) {    // Just change it, and echo    $GLOBALS['mode'] = FTP_ASCII;    echo "- Transfer mode: ASCII\n";}// To Change the transfer mode to Binaryfunction _binary($opts) {    // Just change it, and echo    $GLOBALS['mode'] = FTP_BINARY;    echo "- Transfer mode: Binary\n";}function _bin($opts) {    _binary($opts);}// Allow for deleting of filesfunction _delete($opts) {    // If we don't have any options, give an error    if (!($opts)) {        echo "! Error, no file specified\n";    } else {        // Now, attempt to delete the file        if (!@ftp_delete($GLOBALS['server'], $opts[0])) {            echo "! Error - Could not delete file\n";        } else {            echo "- File Deleted: {$opts[0]}\n";        }    }}function _del($opts) {    _delete($opts);}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?