Php implements ftp upload class and call example

Source: Internet
Author: User
Tags crc32 ftp connection
Php implements ftp upload class and call example
An ftp upload class, similar to an online example, is very simple and suitable for reference by new users.

1. file Upload ftp. class. php

     FtpUrl = $ ftpUrl;} if ($ ftpUser) {$ this-> ftpUser = $ ftpUser;} if ($ ftpPass) {$ this-> ftpPass = $ ftpPass ;} if ($ ftpUrl) {$ this-> ftpDir = $ ftpDir;} if ($ this-> ftpR = ftp_connect ($ this-> ftpUrl, 21 )) {if (ftp_login ($ this-> ftpR, $ this-> ftpUser, $ this-> ftpPass) {if (! Empty ($ this-> ftpDir) {ftp_chdir ($ this-> ftpR, $ this-> ftpDir);} ftp_pasv ($ this-> ftpR, true ); // R enables passive mode; $ status = 1;} else {$ status = 3 ;}} else {$ status = 2 ;}// R switches the directory; function cd ($ dir) {return ftp_chdir ($ this-> ftpR, $ dir);} // R returns the current path strength; function pwd () {return ftp_pwd ($ this-> ftpR);} // R creates the directory function mkdir ($ directory) {return ftp_mkdir ($ this-> ftpR, $ directory );} // R deletes the function rmdi directory. R ($ directory) {return ftp_rmdir ($ this-> ftpR, $ directory);} // R upload a file; function put ($ localFile, $ remoteFile = '') {if ($ remoteFile = '') {$ remoteFile = end (explode ('/', $ localFile);} $ res = ftp_nb_put ($ this-> ftpR, $ remoteFile, $ localFile, FTP_BINARY); while ($ res = FTP_MOREDATA) {$ res = ftp_nb_continue ($ this-> ftpR);} if ($ res = FTP_FINISHED) {return true;} elseif ($ res = FTP_FAILED) {r Eturn false ;}}// R download file; function get ($ remoteFile, $ localFile = '') {if ($ localFile = '') {$ localFile = end (explode ('/', $ remoteFile);} if (ftp_get ($ this-> ftpR, $ localFile, $ remoteFile, FTP_BINARY )) {$ flag = true;} else {$ flag = false;} return $ flag;} // R file size; function size ($ file) {return ftp_size ($ this-> ftpR, $ file);} // whether the R file exists; function isFile ($ file) {if ($ this-> size ($ file)> = 0) {Return true;} else {return false ;}// R file Time function fileTime ($ file) {return ftp_mdtm ($ this-> ftpR, $ file );} // R delete the file; function unlink ($ file) {return ftp_delete ($ this-> ftpR, $ file );} function nlist ($ dir = '/service/resource/') {return ftp_nlist ($ this-> ftpR, $ dir);} // R close the connection; function bye () {return ftp_close ($ this-> ftpR) ;}}?>

2. call example uplaod. php

     Put ($ localfile, $ remotefile); // FTP uploads the source image to the remote server $ ftp-> bye (); // closes the FTP connection unlink ($ workDir. "/". $ img_name) or die ("Cannot delete uploaded file from working directory -- manual deletion recommended"); if (! $ Ftpput) {return false;} else {return $ img_path. '/'. $ img_name;}?>

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.