Using php to write ftp file upload function tutorial _ PHP Tutorial

Source: Internet
Author: User
Tags ftp file ftp login ftp client
Use php to write an ftp file upload function tutorial. This tutorial is a tutorial on using php to write ftp file upload functions. php is very powerful and can use its own functions to instance ftp functions, implement the file upload function. this tutorial is a tutorial on using php to write ftp file upload functions. php is very powerful, you can use functions provided by Alibaba Cloud to implement ftp functions.

This tutorial is a tutorial on using php to write ftp file upload functions. php is very powerful and can use its own functions to instance ftp functions, implement the file upload function. The following is a complete instance.

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 "n N ";
Echo "n ";
Echo"N ";
Echo"N ";
}

Function phpftp_login (){
Phpftp_top ();
Echo" N ";
Echo"

N ";
Echo "n ";
Echo"

N ";
Phpftp_bottom ();
}

/* This function does not return TRUE/FALSE-it returns the value
$ 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"



















Directory empty or not readable

N ";
}
Echo"

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" Nn ";} Else {Echo"
N ";
Echo"Directories
N ";
Echo "n ";
Echo"
Nn ";
Echo"Files
N ";
Echo "n ";
Echo"
Nn ";
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;
}

?>


Ftp file upload function tutorial. php is very powerful. you can use its own functions to instance ftp functions and implement file upload functions. this tutorial...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.