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 ); ?> |