PHP cross-domain cross-server upload file instance tutorials across servers

Source: Internet
Author: User

How do I upload files across servers across a domain across a network? The most basic way to upload is:

1. Users upload files to the Web server

2. Web server uses the Move_uploaded_file () function to move files to the specified folder

However, sometimes we need to put the uploaded files to another dedicated file server, this time, we can not use Move_uploaded_file () to move files, and need to use FTP to transfer files to the file server, the method is very simple ...

See Code directly:

The code is as follows Copy Code

$file = $_files[' file '];

$file _tmp = $file [' Tmp_name '];

$file _name = $file [' name '];

if (Is_uploaded_file ($file _tmp)) {//Make sure the user has an "upload" file

$file _ext = STRRCHR ($file _name, '. '); File names of files to upload

$file _name_new = Date (' Ymdhis '). $file _ext;

$host = ' 127.0.0.1 ';

$port = ' 21 ';

$user = ' admin ';

$pass = ' 123456 ';

$link = Ftp_connect ($host, $port);

$login = Ftp_login ($link, $user, $pass);

Ftp_chdir ($link, ' filedir '); Switch to the folder where you want to put the file

if (Ftp_put ($link, $file _name_new, $file _tmp,ftp_binary)) {

$msg = ' upload success ';

}else{

$msg = ' upload failed ';

}

}else{

$msg = ' upload failed ';

}

Ftp_close ($link);

Echo $msg;

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.