Tutorial on writing ftp File Upload functions using php

Source: Internet
Author: User
Tags ftp file ftp login ftp client

This tutorial is a tutorial on using php to write ftp File Upload functions. php is very powerful. You can use its own functions to instance ftp functions and implement File Upload 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 "<! -- Function phpftp_top --> n ";
Echo "Echo "Echo "<title> php ftp Client v1.4 </title> n ";
Echo "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 "}

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
$ 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> 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> 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> 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> 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> 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> 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;
}

?>


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.