本篇教程是一款關於用php寫 ftp文件上傳函數教程哦,php功能很強大,可以用他自身帶的函數來實例ftp的功能,實現文件上傳功能哦
本篇教程是一款關於用php教程寫 ftp文件上傳函數教程哦,php功能很強大,可以用他自身帶的函數來實例ftp的功能,實現文件上傳功能哦。下面是一個完整的實例
<?
function phpftp_top() {
echo "<!-- function phpftp_top -->n";
echo "<html>n";
echo "<head>n";
echo "<title>PHP FTP Client v1.4</title>n";
echo "</head>n";
echo "<body bgcolor="#ffffff">n";
echo "<font face="courier">nn";
}function phpftp_bottom() {
echo "n<!-- function phpftp_bottom -->n";
echo "</font>n";
echo "</body>n";
echo "</html>n";
}function phpftp_login() {
phpftp_top();
echo "<!-- function phpftp_login -->n";
echo "<p>n";
echo "<form action="ftp.php3" method=post>n";
echo "Login: ";
echo "<input name="phpftp_user" type="text">n";
echo "<p>n";
echo "Password: ";
echo "<input name="phpftp_passwd" type="password">n";
echo "<p>n";
echo "Directory: ";
echo "<input name="phpftp_dir" type="text">n";
echo "<p>n";
echo "<input type="hidden" name="function" value="dir">n";
echo "<input type="submit" value="connect">n";
echo "</form>n";
echo "<p>n";
phpftp_bottom();
}/* This function does not return TRUE/FALSE - it returns the value of
$ftp, the current FTP stream. */function phpftp_connect( $phpftp_user, $phpftp_passwd ) {
$phpftp_host="localhost";
$ftp = ftp_connect( $phpftp_host );
if ( $ftp ) {
if ( ftp_login( $ftp, $phpftp_user, $phpftp_passwd ) ) {
return $ftp;
}
}
}function phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir ) {
phpftp_top();
echo "<!-- function phpftp_list -->n";
$ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
if ( ! $ftp ) {
echo "<strong>FTP login failed!</strong>n";
echo "<a href="ftp.php3">Start over?</a>n";
phpftp_bottom();
} else {
if ( ! $phpftp_dir ) {
$phpftp_dir=ftp_pwd( $ftp );
}
if ( ! @ftp_chdir( $ftp, $phpftp_dir ) ) {
echo "<font color="#ff0000"><strong>Can't enter that directory!</strong></font><p><p>n";
$phpftp_dir=ftp_pwd( $ftp );
}
echo "<strong>Current directory:</strong> " . $phpftp_dir . "
n";
if ( $phpftp_dir == "/" ) {
$phpftp_dir="";
}
echo "<table border=0 cellspacing=20>n";
if ( $contents = ftp_nlist( $ftp, "." ) ) {
$d_i=0;
$f_i=0;
sort($contents);
for ( $i=0; $i < count( $contents ); $i++ ) {
$file_size = ftp_size( $ftp, $contents[$i] );
if ( $file_size == -1 ) {
$nlist_dirs[$d_i]=$contents[$i];
$d_i++;
} else {
$nlist_files[$f_i]=$contents[$i];
$nlist_filesize[$f_i]=$file_size;
$f_i++;
}
}
echo "<tr><td>n";
echo "<font face="courier"><strong>Directories</strong></font>
n";
echo "<form action="ftp.php3" method=post>n";
echo "<input type="hidden" name="function" value="cd">n";
echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . "">n";
echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . "">n";
echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . "">n";
echo "<select name="select_directory" size="10" width="100">n";
for ( $i=0; $i < count( $nlist_dirs ); $i++ ) {
echo "<option value="" . $nlist_dirs[$i] . "">" . $nlist_dirs[$i] . "</option>n";
}
echo "</select>
n";
echo "<input type="submit" value="Enter Directory">n";
echo "</form>n";
echo "</td><td>nn";
echo "<font face="courier"><strong>Files</strong></font>
n";
echo "<form action="ftp.php3" method=post>n";
echo "<input type="hidden" name="function" value="get">n";
echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . "">n";
echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . "">n";
echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . "">n";
echo "<select name="select_file" size="10">n";
for ( $i=0; $i < count( $nlist_files ); $i++ ) {
echo "<option value="" . $nlist_files[$i] . "">" . $nlist_files[$i] ." ($nlist_filesize[$i] bytes)". "</option>n";
}
echo "</select>
n";
echo "<input type="submit" value="Download File">n";
echo "</form>n";
echo "</td></tr>nn";
} else {
echo "<p><font color="#ff0000"><strong>Directory empty or not readable</strong></font><p>n";
}
echo "</table>nn";
echo "<p>n";
echo "<form action="ftp.php3" method=post>n";
$cdup=dirname( $phpftp_dir );
if ( $cdup == "" ) {
$cdup="/";
}
echo "<input type="hidden" name="function" value="dir">n";
echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . "">n";
echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . "">n";
echo "<input type="hidden" name="phpftp_dir" value="" . $cdup . "">n";
echo "<input type="submit" value="Go up one directory">n";
echo "</form>n";
echo "<p>n";
echo "<form enctype="multipart/form-data" action="ftp.php3" method=post>n";
echo "<input type="hidden" name="max_file_size" value="1000000">n";
echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . "">n";
echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . "">n";
echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . "">n";
echo "<input type="hidden" name="function" value="put">n";
echo "<input type="submit" value="Upload this:">n";
echo "<input name="userfile" type="file">n";
echo "</form>n";
echo "<p>nn";
echo "<form action="ftp.php3" method=post>n";
echo "<input type="hidden" name="function" value="mkdir">n";
echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . "">n";
echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . "">n";
echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . "">n";
echo "<input type="submit" value="Make subdirectory:">n";
echo "<input name="new_dir" type="text">n";
ftp_quit( $ftp );
phpftp_bottom();
}
}function phpftp_cd( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_directory ) {
echo "<!-- function phpftp_cd -->n";
$new_directory=$phpftp_dir . "/" . $select_directory;
phpftp_list( $phpftp_user, $phpftp_passwd, $new_directory );
}function phpftp_mkdir( $phpftp_user, $phpftp_passwd, $phpftp_dir, $new_dir ) {
echo "<!-- function phpftp_mkdir -->n";
$ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
if ( $phpftp_dir == "" ) {
$phpftp_dir="/";
}
if ( ! $ftp ) {
@ftp_quit( $ftp );
phpftp_top();
echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>n";
echo "<a href="ftp.php3">Start over?</a>n";
phpftp_bottom();
} else {
$dir_path = $phpftp_dir . "/" . $new_dir;
@ftp_mkdir( $ftp, $dir_path );
@ftp_quit( $ftp );
phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir );
}
};
function phpftp_get( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_file ) {
$ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
if ( $phpftp_dir == "" ) {
$phpftp_dir="/";
}
if ( ( ! $ftp ) || ( ! @ftp_chdir( $ftp, $phpftp_dir ) ) ) {
@ftp_quit( $ftp );
phpftp_top();
echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>n";
echo "<a href="ftp.php3">Start over?</a>n";
phpftp_bottom();
} else {
srand((double)microtime()*1000000);
$randval = rand();
$tmpfile="/var/tmp/xfers/" . $select_file . "." . $randval;
if ( ! ftp_get( $ftp, $tmpfile, $select_file, FTP_BINARY ) ) {
ftp_quit( $ftp );
phpftp_top();
echo "<font color="#ff0000"><strong>FTP get failed!</strong></font><p><p>n<a href="ftp.php3">Start over?</a>n";
phpftp_bottom();
} else {
ftp_quit( $ftp );
header( "Content-Type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=" . $select_file );
readfile( $tmpfile );
}
@unlink( $tmpfile );
}
}function phpftp_put( $phpftp_user, $phpftp_passwd, $phpftp_dir, $userfile, $userfile_name ) {
srand((double)microtime()*1000000);
$randval = rand();
$tmpfile="/var/tmp/xfers/" . $userfile_name . "." . $randval;
if ( ! @copy( $userfile, $tmpfile ) ) {
phpftp_top();
echo "<font color="#ff0000"><strong>Upload failed! Probably no space on server for temp file.</strong></font><p><p>n<a href="ftp.php3">Start over?</a>n";
phpftp_bottom();
} else {
if ( ! $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd ) ) {
unlink( $tmpfile );
phpftp_top();
echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>n<a href="ftp.php3">Start over?</a>n";
phpftp_bottom();
} else {
ftp_put( $ftp, $userfile_name, $tmpfile, FTP_BINARY );
ftp_quit( $ftp );
unlink( $tmpfile );
phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir );
}
}
}switch( $function ) {
case "dir";
phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir );
break;
case "cd";
phpftp_cd( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_directory );
break;
case "get";
phpftp_get( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_file );
break;
case "put";
phpftp_put( $phpftp_user, $phpftp_passwd, $phpftp_dir, $userfile, $userfile_name );
break;
case "mkdir";
phpftp_mkdir( $phpftp_user, $phpftp_passwd, $phpftp_dir, $new_dir );
break;
case "";
phpftp_login();
break;
}?>