PHP File Upload to another server-PHP source code

Source: Internet
Author: User
Tags ftp connection ftp file php file upload
In php, if we want to upload data to another server, we need to use the ftp function of php to provide ftp_connect in php to connect to the server, and then upload the uploaded data to the remote server, let me introduce it to you. In php, if we want to upload data to another server, we need to use the ftp function of php to provide ftp_connect in php to connect to the server, and then upload the uploaded data to the remote server, let me introduce it to you.

Script ec (2); script

The ftp_connect () function creates a new FTP connection.

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

Syntax


Host is required. Specifies the FTP server to be connected. It can be a domain name or an IP address.
It should not end with a slash, and it does not need to start with ftp.

Port is optional. Specifies the port number of the FTP server.

Timeout is optional. Specifies the timeout time of the FTP server. The default value is 90 seconds.


Then a test is performed locally on the desktop:

The Code is as follows:
$ Ftp_server = "192.168.1.100 ";
$ Ftp_user_name = "laohu ";
$ Ftp_user_pass = "123456 ";
$ Conn_id = ftp_connect ($ ftp_server) or die ("Couldn't connect to $ ftp_server ");
$ File = 'test.txt ';
$ Remote_file = '/test/a.txt ';

// Note: "/test" refers to the test folder under the ftp root directory, rather than

Test

The Code is as follows:

$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );

If (ftp_put ($ conn_id, $ remote_file, $ file, FTP_BINARY )){
Echo "file moved successfully ";
} Else {
Echo "failed to move ";
}
Ftp_close ($ conn_id );

After running: The file is successfully moved.

This is what we need. Then we use a desktop as a program server. When uploading attachments, we use ftp to upload them to the notebook. The Notebook ip address is 105.

The Code is as follows:

The Code is as follows:

If (is_uploaded_file ($ _ FILES ['uploadfile'] ['tmp _ name']) {
$ Ftp_server = "192.168.1.105 ";
$ Ftp_user_name = "lesley ";
$ Ftp_user_pass = "123456 ";
$ Conn_id = ftp_connect ($ ftp_server) or die ("Couldn't connect to $ ftp_server ");
$ File = $ _ FILES ['uploadfile'] ['tmp _ name'];
$ Remote_file = '/test/'. $ _ FILES ['uploadfile'] ['name'];
$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );

If (ftp_put ($ conn_id, $ remote_file, $ file, FTP_BINARY )){
Echo "file:". $ _ FILES ['uploadfile'] ['name']. "uploaded successfully ";
} Else {
Echo "Upload Failed ";
}
Ftp_close ($ conn_id );
}

The corresponding front-end Page code:

The Code is as follows:

The operation is successful.

Next, let's look at an ftp File Upload class.

The Code is as follows:

/**
* Desc: FTP class
* Link: www.111cn.net
* Date:
*/
Class ftp
{
Public $ off; // return the operation status (successful/failed)
Public $ conn_id; // FTP connection

/**
* Method: FTP connection
* @ FTP_HOST -- FTP host
* @ FTP_PORT -- Port
* @ FTP_USER -- User Name
* @ FTP_PASS -- Password
*/
Function _ construct ($ FTP_HOST, $ FTP_PORT, $ FTP_USER, $ FTP_PASS)
{
$ This-> conn_id = @ ftp_connect ($ FTP_HOST, $ FTP_PORT) or die ("FTP server connection failed ");
@ Ftp_login ($ this-> conn_id, $ FTP_USER, $ FTP_PASS) or die ("FTP Server login failed ");
@ Ftp_pasv ($ this-> conn_id, 1); // enable passive Simulation
}

/**
* Method: upload a file.
* @ Path -- local path
* @ Newpath -- upload path
* @ Type -- New if the target directory does not exist
*/
Function up_file ($ path, $ newpath, $ type = true)
{
If ($ type) $ this-> dir_mkdirs ($ newpath );
$ This-> off = @ ftp_put ($ this-> conn_id, $ newpath, $ path, FTP_BINARY );
If (! $ This-> off) echo "File Upload Failed. Check whether the permission and path are correct! ";
}

/**
* Method: Move a file
* @ Path -- original path
* @ Newpath -- New Path
* @ Type -- New if the target directory does not exist
*/
Function move_file ($ path, $ newpath, $ type = true)
{
If ($ type) $ this-> dir_mkdirs ($ newpath );
$ This-> off = @ ftp_rename ($ this-> conn_id, $ path, $ newpath );
If (! $ This-> off) echo "failed to move the file. Check whether the permission and original path are correct! ";
}

/**
* Method: copy an object
* Note: Because FTP does not have a copy command, the work und of this method is to download the file and upload it to a new path.
* @ Path -- original path
* @ Newpath -- New Path
* @ Type -- New if the target directory does not exist
*/
Function copy_file ($ path, $ newpath, $ type = true)
{
$ Downpath = "c:/tmp. dat ";
$ This-> off = @ ftp_get ($ this-> conn_id, $ downpath, $ path, FTP_BINARY); // download
If (! $ This-> off) echo "failed to copy the file. Check whether the permission and original path are correct! ";
$ This-> up_file ($ downpath, $ newpath, $ type );
}

/**
* Method: delete an object.
* @ Path -- path
*/
Function del_file ($ path)
{
$ This-> off = @ ftp_delete ($ this-> conn_id, $ path );
If (! $ This-> off) echo "An error occurred while deleting the file. Check whether the permission and path are correct! ";
}

/**
* Method: generate a directory
* @ Path -- path
*/
Function dir_mkdirs ($ path)
{
$ Path_arr = explode ('/', $ path); // retrieve the directory Array
$ File_name = array_pop ($ path_arr); // the file name is displayed.
$ Path_p = count ($ path_arr); // number of layers

Foreach ($ path_arr as $ val) // create a directory
{
If (@ ftp_chdir ($ this-> conn_id, $ val) = FALSE)
{
$ Tmp = @ ftp_mkdir ($ this-> conn_id, $ val );
If ($ tmp = FALSE)
{
Echo "Directory creation failed. Please check whether the permission and path are correct! ";
Exit;
}
@ Ftp_chdir ($ this-> conn_id, $ val );
}
}

For ($ I = 1; $ I <= $ path_p; $ I ++) // roll back to the root
{
@ Ftp_cdup ($ this-> conn_id );
}
}

/**
* Method: Disable the FTP connection.
*/
Function close ()
{
@ Ftp_close ($ this-> conn_id );
}
}
// Class class_ftp end
?>

Call Code

The Code is as follows:

/***
* Desc: Call example
* Link: www.111cn.net
* Date:
*/
$ Ftp = new ftp ('192. 168.0.249 ', 21, 'hlj', '123'); // open the FTP connection
$ Ftp-> up_file('aa.wav ', 'test/13548957217/bb.wav'); // upload a file
// $ Ftp-> move_file ('aaa/aaa. php', 'aaa. php'); // move the file
// $ Ftp-> copy_file ('aaa. php', 'aaa/aaa. php'); // copy the file
// $ Ftp-> del_file ('aaa. php'); // delete an object
$ Ftp-> close (); // close the FTP connection
?>

Note that the fourth parameter transfer mode requires FTP_BINARY (binary mode) and FTP_ASCII (text mode) When ftp_put is used)

Images can be uploaded but cannot be displayed. Other files are renamed, Chinese characters are garbled, and upload permission control is not mentioned here.

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.