Introduction to PHP file uploading through FTP

Source: Internet
Author: User
Tags explode ftp ftp connection php file

The ftp_get () function downloads an object from the FTP server.

If the call succeeds, true is returned. If the call fails, false is returned.

Syntax
Ftp_get (ftp_connection, local, remote, mode, resume)


The ftp_connect () function creates a new FTP connection.

If the connection succeeds, a connection id is returned. Otherwise, false is returned.

Syntax
Ftp_connect (host, port, timeout)


The ftp_login () function is used to log on to the FTP server.

If the call succeeds, true is returned. If the call fails, false is returned and a warning is issued.

Syntax
Ftp_login (ftp_connection, username, password)

The three functions have been introduced, and now we start.

Example 1

The code is as follows: Copy code

$ Ftp_server = "*.*.*.*";
$ Ftp_user = "lu ";
$ Ftp_pass = "love you ";

// Set up a connection or die
$ Conn_id = ftp_connect ($ ftp_server) or die ("Couldn't connect to $ ftp_server ");

$ Login_result = ftp_login ($ conn_id, $ ftp_user, $ ftp_pass );

If ((! $ Conn_id) | (! $ Login_result )){
Echo "FTP connection has failed! ";
Echo "Attempted to connect to $ ftp_server for user $ ftp_user_name ";
Exit;
} Else {
Echo "Connected to $ ftp_server, for user $ ftp_user_name ";
    }

// Try to login
$ Filename = date ('ymd'). ". xml ";
$ Source_file = "/usr/local/IVR/sendwireless/xml/data/". $ filename; // source address
Echo $ source_file;
$ Destination_file = "/ITC/admin/logstat/ftplog/". $ filename; // target address
$ Upload = ftp_put ($ conn_id, $ destination_file, $ source_file, FTP_BINARY) or die ("Couldn't connect to $ ftp_server ");
Ftp_quit ($ conn_id );

If (! $ Upload ){
Echo "FTP upload has failed! ";
} Else {
Echo "Uploaded $ source_file to $ ftp_server as $ destination_file ";
    }
Ftp_close ($ conn_id );


Upload files to the local device for necessary modifications, such as adding watermarks, and then upload files to the remote server through FTP.

Example 2:

The code is as follows: Copy code

// Upload an image

If ($ _ FILES ['Pic '] ['name'])
{
$ File_path = '/opt/www/img /';
$ Pic = upload ('Pic ', $ filename, 'jpg | jpeg | gif | bmp | png', $ file_path );
If (! $ Pic)
{
Echo "image Upload failed! ";
Exit;
}
Require_once (ROOT_PATH. 'Lib/Class/Ftp. class. Php ');
$ Ftp = new ftp ("127.0.0.1", "gamezeroftp", "123456", "/opt/www ");
$ Localfile = '/opt/www/img/'. $ pic;
$ Remotefile = '/opt/www/gamepics/'. $ pic;
$ Ftpput = $ ftp-> put ($ localfile, $ remotefile); // upload the source image to the remote server through FTP
If (! $ Ftpput ){
Echo "an error occurred while uploading images to the remote server! ";
}
$ Ftp-> bye (); // Close the FTP connection
}
Attached FTP operation class:


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 switch 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 a directory
Function mkdir ($ directory ){
Return ftp_mkdir ($ this-> ftpR, $ directory );
}
// R delete the Directory
Function rmdir ($ directory ){
Return ftp_rmdir ($ this-> ftpR, $ directory );
}
// R uploads 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 ){
Return false;
}
}
// R download the 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 an object;
Function unlink ($ file ){
Return ftp_delete ($ this-> ftpR, $ file );
}
Function nlist ($ dir = '/service/resource /'){
Return ftp_nlist ($ this-> ftpR, $ dir );
}
// R closes the connection;
Function bye (){
Return ftp_close ($ this-> ftpR );
}
}
?>

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.