> 백엔드 개발 > PHP 튜토리얼 > 用php写 ftp文件上传函数教程_PHP教程

用php写 ftp文件上传函数教程_PHP教程

WBOY
풀어 주다: 2016-07-20 11:07:32
원래의
820명이 탐색했습니다.

本篇教程是一款关于用php写 ftp文件上传函数教程哦,php功能很强大,可以用他自身带的函数来实例ftp的功能,实现文件上传功能哦

本篇教程是一款关于用php教程写 ftp文件上传函数教程哦,php功能很强大,可以用他自身带的函数来实例ftp的功能,实现文件上传功能哦。下面是一个完整的实例


function phpftp_top() {
echo "n";
echo "n";
echo "

n";
echo "PHP FTP Client v1.4n";
echo "n";
echo "n";
echo "nn";
}

function phpftp_bottom() {
echo "nn";
echo "

n";
echo "n";
echo "n";
}

function phpftp_login() {
phpftp_top();
echo "n";
echo "

n";
echo "

n";
echo "Login:     ";
echo "n";
echo "

n";
echo "Password:  ";
echo "n";
echo "

n";
echo "Directory: ";
echo "n";
echo "

n";
echo "n";
echo "n";
echo "

n";
echo "

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 "n";
$ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
if ( ! $ftp ) {
echo "FTP login failed!n";
echo "Start over?n";
phpftp_bottom();
} else {
if ( ! $phpftp_dir ) {
$phpftp_dir=ftp_pwd( $ftp );
}
if ( ! @ftp_chdir( $ftp, $phpftp_dir ) ) {
echo "Can't enter that directory!

n";
$phpftp_dir=ftp_pwd( $ftp );
}
echo "Current directory: " . $phpftp_dir . "
n";
if ( $phpftp_dir == "/" ) {
$phpftp_dir="";
}
echo "

n";
if ( $contents = ftp_nlist( $ftp, "." ) ) {
$d_i=0;
$f_i=0;
sort($contents);
for ( $i=0; $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 "nn";
} else {
echo "

Directory empty or not readable

n";
}
echo "

n";
echo "Directories
n";
echo "
n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "
n";
echo "n";
echo "
n";
echo "
nn";
echo "Files
n";
echo "
n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "
n";
echo "n";
echo "
n";
echo "
nn";
echo "

n";
echo "

n";
$cdup=dirname( $phpftp_dir );
if ( $cdup == "" ) {
$cdup="/";
}
echo "n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "
n";
echo "

n";
echo "

n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "
n";
echo "

nn";
echo "

n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "n";
ftp_quit( $ftp );
phpftp_bottom();
}
}

function phpftp_cd( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_directory ) {
echo "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 "n";
$ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
if ( $phpftp_dir == "" ) {
$phpftp_dir="/";
}
if ( ! $ftp ) {
@ftp_quit( $ftp );
phpftp_top();
echo "FTP login failed!

n";
echo "Start over?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 "FTP login failed!

n";
echo "Start over?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 "FTP get failed!

nStart over?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 "Upload failed! Probably no space on server for temp file.

nStart over?n";
phpftp_bottom();
} else {
if ( ! $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd ) ) {
unlink( $tmpfile );
phpftp_top();
echo "FTP login failed!

nStart over?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;
}

?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444933.htmlTechArticle本篇教程是一款关于用php写 ftp文件上传函数教程哦,php功能很强大,可以用他自身带的函数来实例ftp的功能,实现文件上传功能哦 本篇教程...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿