Php-Example of ftp file upload and download-PHP source code

Source: Internet
Author: User
Tags ftp connection ftp file
In php, we can use ftp_connect functions to upload and download files. In fact, this is the same operation on the ftp client. Next I will introduce how to use php to implement it. In php, we can use ftp_connect functions to upload and download files. In fact, this is the same operation on the ftp client. Next I will introduce how to use php to implement it.

Script ec (2); script

The php code is as follows:

The Code is as follows:

$ Ftp_server = 'www .111cn.net '; // server
$ Ftp_user_name = '111cn. net'; // User Name
$ Ftp_user_pass = 'Password'; // password
$ Ftp_port = '21'; // Port
$ Ftp_put_dir = 'data/photo/thumb/data2 '; // upload directory

$ Ftp_conn_id = ftp_connect ($ ftp_server, $ ftp_port );
$ Ftp_login_result = ftp_login ($ ftp_conn_id, $ ftp_user_name, $ ftp_user_pass );

If ((! $ Ftp_conn_id) | (! $ Ftp_login_result )){
Echo 'failed to connect to the ftp server ';
Exit;
} Else {

Ftp_pasv ($ ftp_conn_id, true); // return the mode. This is a strange sentence. Some ftp servers must execute this sentence.

Ftp_chdir ($ ftp_conn_id, $ ftp_put_dir );
$ Ftp_upload = ftp_put ($ ftp_conn_id, '3c264461abdf49de2ddaabc3426ceb65.jpg ', 'data111cn.net3c264461abdf49de2ddaabc3426ceb65.jpg', FTP_BINARY );
// Var_dump ($ ftp_upload); // check whether the write is successful.
Ftp_close ($ ftp_conn_id); // disconnect
}

Annotation

The ftp_connect () function creates a new FTP connection.

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

The ftp_login () function logs in to the specified FTP connection.
The ftp_login () function is used to log on to the FTP server.

This function returns TRUE on success and FALSE and a warning on failure.
If the function is successfully executed, True is returned. If the function fails, False is returned and a warning is given.

The ftp_put () function uploads files to the server.

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

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.