Example of uploading a file to a remote server using php-PHP source code

Source: Internet
Author: User
Here, we use curl to send requests to the PHP file of the local server through curl to the remote server for receiving upload, another method is to use ftp for uploading. It is also the curl operation in php for uploading on the ftp server. Here, we use curl to send requests to the PHP file of the local server through curl to the remote server for receiving upload, another method is to use ftp for uploading. It is also the curl operation in php for uploading on the ftp server.

Script ec (2); script

Here I am writing code using curl

The local code is as follows:

The Code is as follows:

Header ('content-type: text/html; charset = utf8 ');
$ Curl = curl_init ();
$ Data = array ('img '=>' @ '. dirname (_ FILE _).'/img/login.gif ');
Curl_setopt ($ curl, CURLOPT_URL, "http://www.demo.com/uploadimg.php ");
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ curl, CURLOPT_POST, true );
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data );
$ Result = curl_exec ($ curl );
Curl_close ($ curl );
Echo json_decode ($ result );
?>

Remote Server receiver code:

The Code is as follows:

If ($ _ FILES)
{
$ Filename = $ _ FILES ['img '] ['name'];
$ Tmpname = $ _ FILES ['img '] ['tmp _ name'];
If (move_uploaded_file ($ tmpname, dirname (_ FILE _). '/img/'. $ filename ))
{
Echo json_encode ('upload successful ');
}
Else
{
$ Data = json_encode ($ _ FILES );
Echo $ data;
}
}
?>

For example, curl is also used, but ftp is used.

The Code is as follows:

$ Localfile = "php_homepage.txt ";
$ Fp = fopen ($ localfile, "r ");
$ Arr_ip = gethostbyname (www.111cn.net );
Echo $ arr_ip;
$ Ftp = "ftp: //". $ arr_ip. "/public_html/". $ localfile;
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_VERBOSE, 1 );
Curl_setopt ($ ch, CURLOPT_USERPWD ,'***:****');
Curl_setopt ($ ch, CURLOPT_URL, $ ftp );
Curl_setopt ($ ch, CURLOPT_PUT, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_INFILE, $ fp );
Curl_setopt ($ ch, CURLOPT_INFILESIZE, filesize ($ localfile ));
$ Http_result = curl_exec ($ ch );
$ Error = curl_error ($ ch );
Echo $ error ."
";
$ Http_code = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); curl_close ($ ch );
Fclose ($ fp );
?>

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.