|
<br> 此程序需要FTP函数支持。 <BR>原文件 http://inebria.com/phpftp/ftp.phps <BR>请注意为了节省篇幅,这里省略了作者的版权声明 <BR><BR>ftp.php3 <BR><BR><?php <BR><BR>function phpftp_top() { <BR> echo "<!-- function phpftp_top --> "; <BR> echo "<html> "; <BR> echo "<head> "; <BR> echo "<title> HP FTP Client v1.4</title> "; <BR> echo "</head> "; <BR> echo "<body bgcolor="#ffffff"> "; <BR> echo "<font face="courier"> "; <BR>} <BR><BR>function phpftp_bottom() { <BR> echo " <!-- function phpftp_bottom --> "; <BR> echo "</font> "; <BR> echo "</body> "; <BR> echo "</html> "; <BR>} <BR><BR>function phpftp_login() { <BR> phpftp_top(); <BR> echo "<!-- function phpftp_login --> "; <BR> echo "<p> "; <BR> echo "<form action="ftp.php3" method=post> "; <BR> echo "Login: "; <BR> echo "<input name="phpftp_user" type="text"> "; <BR> echo "<p> "; <BR> echo " assword: "; <BR> echo "<input name="phpftp_passwd" type="password"> "; <BR> echo "<p> "; <BR> echo "Directory: "; <BR> echo "<input name="phpftp_dir" type="text"> "; <BR> echo "<p> "; <BR> echo "<input type="hidden" name="function" value="dir"> "; <BR> echo "<input type="submit" value="connect"> "; <BR> echo "</form> "; <BR> echo "<p> "; <BR> phpftp_bottom(); <BR>} <BR><BR>/* This function does not return TRUE/FALSE - it returns the value of <BR> $ftp, the current FTP stream. */ <BR><BR>function phpftp_connect( $phpftp_user, $phpftp_passwd ) { <BR> $phpftp_host= "localhost"; <BR> $ftp = ftp_connect( $phpftp_host ); <BR> if ( $ftp ) { <BR> if ( ftp_login( $ftp, $phpftp_user, $phpftp_passwd ) ) { <BR> return $ftp; <BR> } <BR> } <BR>} <BR><BR>function phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir ) { <BR> phpftp_top(); <BR> echo "<!-- function phpftp_list --> "; <BR> $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd ); <BR> if ( ! $ftp ) { <BR> echo "<strong>FTP login failed!</strong> "; <BR> echo "<a href="ftp.php3">Start over?</a> "; <BR> phpftp_bottom(); <BR> } else { <BR> if ( ! $phpftp_dir ) { <BR> $phpftp_dir=ftp_pwd( $ftp ); <BR> } <BR> if ( ! @ftp_chdir( $ftp, $phpftp_dir ) ) { <BR> echo "<font color="#ff0000"><strong>Can't enter that directory!</strong></font><p><p> "; <BR> $phpftp_dir=ftp_pwd( $ftp ); <BR> } <BR> echo "<strong>Current directory:</strong> " . $phpftp_dir . "<br> "; <BR> if ( $phpftp_dir == "/" ) { <BR> $phpftp_dir= ""; <BR> } <BR> echo "<table border=0 cellspacing=20> "; <BR> if ( $contents = ftp_nlist( $ftp, "." ) ) { <BR> $d_i=0; <BR> $f_i=0; <BR> sort($contents); <BR> for ( $i=0; $i < count( $contents ); $i++ ) { <BR> $file_size = ftp_size( $ftp, $contents[$i] ); <BR> if ( $file_size == -1 ) { <BR> $nlist_dirs[$d_i]=$contents[$i]; <BR> $d_i++; <BR> } else { <BR> $nlist_files[$f_i]=$contents[$i]; <BR> $nlist_filesize[$f_i]=$file_size; <BR> $f_i++; <BR> } <BR> } <BR> echo "<tr><td> "; <BR> echo "<font face="courier"><strong>Directories</strong></font><br> "; <BR> echo "<form action="ftp.php3" method=post> "; <BR> echo "<input type="hidden" name="function" value="cd"> "; <BR> echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> "; <BR> echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> "; <BR> echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> "; <BR> echo "<select name="select_directory" size="10" width="100"> "; <BR> for ( $i=0; $i < count( $nlist_dirs ); $i++ ) { <BR> echo "<option value="" . $nlist_dirs[$i] . "">" . $nlist_dirs[$i] . "</option> "; <BR> } <BR> echo "</select><br> "; <BR> echo "<input type="submit" value="Enter Directory"> "; <BR> echo "</form> "; <BR> echo "</td><td> "; <BR> echo "<font face="courier"><strong>Files</strong></font><br> "; <BR> echo "<form action="ftp.php3" method=post> "; <BR> echo "<input type="hidden" name="function" value="get"> "; <BR> echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> "; <BR> echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> "; <BR> echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> "; <BR> echo "<select name="select_file" size="10"> "; <BR> for ( $i=0; $i < count( $nlist_files ); $i++ ) { <BR> echo "<option value="" . $nlist_files[$i] . "">" . $nlist_files[$i] . " ($nlist_filesize[$i] bytes)". "</option> "; <BR> } <BR> echo "</select><br> "; <BR> echo "<input type="submit" value="Download File"> "; <BR> echo "</form> "; <BR> echo "</td></tr> "; <BR> } else { <BR> echo "<p><font color="#ff0000"><strong>Directory empty or not readable</strong></font><p> "; <BR> } <BR> echo "</table> "; <BR> echo "<p> "; <BR> echo "<form action="ftp.php3" method=post> "; <BR> $cdup=dirname( $phpftp_dir ); <BR> if ( $cdup == "" ) { <BR> $cdup= "/"; <BR> } <BR> echo "<input type="hidden" name="function" value="dir"> "; <BR> echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> "; <BR> echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> "; <BR> echo "<input type="hidden" name="phpftp_dir" value="" . $cdup . ""> "; <BR> echo "<input type="submit" value="Go up one directory"> "; <BR> echo "</form> "; <BR> echo "<p> "; <BR> echo "<form enctype="multipart/form-data" action="ftp.php3" method=post> "; <BR> echo "<input type="hidden" name="max_file_size" value="1000000"> "; <BR> echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> "; <BR> echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> "; <BR> echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> "; <BR> echo "<input type="hidden" name="function" value="put"> "; <BR> echo "<input type="submit" value="Upload this:"> "; <BR> echo "<input name="userfile" type="file"> "; <BR> echo "</form> "; <BR> echo "<p> "; <BR> echo "<form action="ftp.php3" method=post> "; <BR> echo "<input type="hidden" name="function" value="mkdir"> "; <BR> echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> "; <BR> echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> "; <BR> echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> "; <BR> echo "<input type="submit" value="Make subdirectory:"> "; <BR> echo "<input name="new_dir" type="text"> "; <BR> ftp_quit( $ftp ); <BR> phpftp_bottom(); <BR> } <BR>} <BR><BR>function phpftp_cd( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_directory ) { <BR> echo "<!-- function phpftp_cd --> "; <BR> $new_directory=$phpftp_dir . "/" . $select_directory; <BR> phpftp_list( $phpftp_user, $phpftp_passwd, $new_directory ); <BR>} <BR><BR>function phpftp_mkdir( $phpftp_user, $phpftp_passwd, $phpftp_dir, $new_dir ) { <BR> echo "<!-- function phpftp_mkdir --> "; <BR> $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd ); <BR> if ( $phpftp_dir == "" ) { <BR> $phpftp_dir= "/"; <BR> } <BR> if ( ! $ftp ) { <BR> @ftp_quit( $ftp ); <BR> phpftp_top(); <BR> echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> "; <BR> echo "<a href="ftp.php3">Start over?</a> "; <BR> phpftp_bottom(); <BR> } else { <BR> $dir_path = $phpftp_dir . "/" . $new_dir; <BR> @ftp_mkdir( $ftp, $dir_path ); <BR> @ftp_quit( $ftp ); <BR> phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir ); <BR> } <BR>}; <BR> <BR><BR>function phpftp_get( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_file ) { <BR> $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd ); <BR> if ( $phpftp_dir == "" ) { <BR> $phpftp_dir= "/"; <BR> } <BR> if ( ( ! $ftp ) || ( ! @ftp_chdir( $ftp, $phpftp_dir ) ) ) { <BR> @ftp_quit( $ftp ); <BR> phpftp_top(); <BR> echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> "; <BR> echo "<a href="ftp.php3">Start over?</a> "; <BR> phpftp_bottom(); <BR> } else { <BR> srand((double)microtime()*1000000); <BR> $randval = rand(); <BR> $tmpfile= "/var/tmp/xfers/" . $select_file . "." . $randval; <BR> if ( ! ftp_get( $ftp, $tmpfile, $select_file, FTP_BINARY ) ) { <BR> ftp_quit( $ftp ); <BR> phpftp_top(); <BR> echo "<font color="#ff0000"><strong>FTP get failed!</strong></font><p><p> <a href="ftp.php3">Start over?</a> "; <BR> phpftp_bottom(); <BR> } else { <BR> ftp_quit( $ftp ); <BR> header( "Content-Type: application/octet-stream" ); <BR> header( "Content-Disposition: attachment; filename=" . $select_file ); <BR> readfile( $tmpfile ); <BR> } <BR> @unlink( $tmpfile ); <BR> } <BR>} <BR><BR>function phpftp_put( $phpftp_user, $phpftp_passwd, $phpftp_dir, $userfile, $userfile_name ) { <BR> srand((double)microtime()*1000000); <BR> $randval = rand(); <BR> $tmpfile= "/var/tmp/xfers/" . $userfile_name . "." . $randval; <BR> if ( ! @copy( $userfile, $tmpfile ) ) { <BR> phpftp_top(); <BR> echo "<font color="#ff0000"><strong>Upload failed! Probably no space on server for temp file.</strong></font><p><p> <a href="ftp.php3">Start over?</a> "; <BR> phpftp_bottom(); <BR> } else { <BR> if ( ! $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd ) ) { <BR> unlink( $tmpfile ); <BR> phpftp_top(); <BR> echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> <a href="ftp.php3">Start over?</a> "; <BR> phpftp_bottom(); <BR> } else { <BR> ftp_put( $ftp, $userfile_name, $tmpfile, FTP_BINARY ); <BR> ftp_quit( $ftp ); <BR> unlink( $tmpfile ); <BR> phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir ); <BR> } <BR> } <BR>} <BR><BR>switch( $function ) { <BR> case "dir"; <BR> phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir ); <BR> break; <BR> case "cd"; <BR> phpftp_cd( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_directory ); <BR> break; <BR> case "get"; <BR> phpftp_get( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_file ); <BR> break; <BR> case "put"; <BR> phpftp_put( $phpftp_user, $phpftp_passwd, $phpftp_dir, $userfile, $userfile_name ); <BR> break; <BR> case "mkdir"; <BR> phpftp_mkdir( $phpftp_user, $phpftp_passwd, $phpftp_dir, $new_dir ); <BR> break; <BR> case ""; <BR> phpftp_login(); <BR> break; <BR>} <BR> <BR>?> <br><br> |
|