PHP Upload the image file to another domain using CURL.

I have two domains, for example site1.loc and site2.loc . In site1.loc , I have a php form file similar to this:

<?php $c_name = ""; $c_phone = ""; if($_SERVER['REQUEST_METHOD']=="POST"){ $c_name = $_POST['c_name']; $c_phone = $_POST['c_phone']; $c_pic = $_FILES['c_pic']['name']; // Image file // submit target URL $url = 'http://site2.loc/handler.php'; $fields = array( 'field1'=>$c_name, 'field2'=>$c_phone, 'field3'=>$c_pic ); $postvars=''; $sep=''; foreach($fields as $key=>$value) { $postvars.= $sep.urlencode($key).'='.urlencode($value); $sep='&'; } //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars); //execute post $result = curl_exec($ch); if(curl_errno($ch)) { echo 'Error: ' . curl_error($ch); } else { echo $result; } //close connection curl_close($ch); } echo ' <form action="" method="post" enctype="multipart/form-data"> Name : <input type="text" name="c_name" value="'.$c_name.'" /> <br /> Phone : <input type="text" name="c_phone" value="'.$c_phone.'" /> <br /> Image : <input type="file" name="c_pic" /> <br /> <input type="submit" /> </form> '; ?> 

and handler.php in site2.loc as follows:

 <?php ob_start(); if (!isset($_SESSION)) { session_start(); } // CONNECT TO DB $db_con = mysql_connect("localhost", "root", "root");// or die("Could not connect to db."); if(!mysql_select_db("site2",$db_con)) die("No database selected."); // POST if(isset($_POST)){ $c_name = $_POST['field1']; $c_phone = $_POST['field2']; $c_pic = $_POST['field3']; // UPLOAD FILE /* UPLOAD IMAGE CODE HERE */ // INSERT TO DB if(mysql_query("INSERT INTO kontak (nama, telpon) VALUES ('$c_name','$c_phone')")){ echo "INSERT SUCCESS"; } else { echo "INSERT FAILED"; } } ?> 

This script works well for storing data in a database, but cannot load an image file. Can someone help me modify the scripts above to upload an image file?

Thanks before.

+4
source share
3 answers

There is no code in this answer, just an example workflow that will bypass curl :

  • The user submits the form with the downloaded file to "site 1", as usual.
  • "site 1" processes the form and the uploaded file. The file is placed in the temp directory, available on the Internet.
  • On "site 1", once the downloaded file has been tagged, call file_get_contents('http://site2.loc/pullfile.php?f=filename&sec=checkcode') . Content pullfile.php will do just that, pull the file from "Site 1" to "site 2".
  • The return from file_get_contents() can be checked for the error return from the "site 2" pullfile.php and, by mistake, can handle this. No error, delete the temporary file.
  • &sec=checkcode can be used to confirm that the file was successfully uploaded to "site 2". It could be an MD5 file or something else that you come up with.

Just an idea.

Change Sample code to help make things more understandable is possible:]

 // ---- Site 1, formprocess.php ---- // Your form processing goes here, including // saving the uploaded file to a temp dir. // Once the uploaded file is checked for errors, // you need move it to a known temp folder called // 'tmp'. // this is the uploaded file name (which you would // have got from the processed form data in $_FILES // For this sample code, it is simple hard-coded. $site1File = 'test.jpg'; $site1FileMd5 = md5_file('./tmp/'.$site1File); // now make a remote request to "site 2" $site2Result = file_get_contents('http://'.SITE_2_URL.'/pullfile.php?f='.$site1File.'&md5='.$site1FileMd5); if ($site2Result == 'done') { unlink('./tmp/'.$site1File); } else { echo '<p> Uploaded file failed to transfer to '.SITE_2_URL.' - but will be dealt with later. </p>'; } 

And that will be "pullfile.php" on site 2

 // ----- Site 2, pullfile.php ----- // This script will pull a file from site 1 and // place it in '/uploaded' // used to cross-check the uploaded file $fileMd5 = $_GET['md5']; $fileName = basename($_GET['f']); // we need to pull the file from the './tmp/' dir on site 1 $pulledFile = file_get_contents('http://'.SITE_1_URL.'/tmp/'.$fileName); // save that file to disk $result = file_put_contents('./uploaded/'.$fileName,$pulledFile); if (! $result) { echo 'Error: problem writing file to disk'; exit; } $pulledMd5 = md5_file('./uploaded/'.$fileName); if ($pulledMd5 != $fileMd5) { echo 'Error: md5 mis-match'; exit; } // At this point, everything should be right. // We pass back 'done' to site 1, so we know // everything went smooth. This way, a 'blank' // return can be treated as an error too. echo 'done'; exit; 
+4
source

CURLOPT_POSTFIELDS Complete data to publish to the HTTP "POST" operation. To publish the file, add the file name with @ and use the full track. This can be passed as a string with urlencoded, for example 'para1 = va1 & para2 = val2 & ...' or as an array with the field name as the key and field data as the value. If the value is an array, the Content-Type header will be set to multipart / form-data.

http://php.net/manual/en/function.curl-setopt.php

Code snippet:

 $postvars['file'] = '@full/path/to/file.jpg'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, count($fields)); curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars); curl_exec($ch); 
+3
source

The way you do it right now will not work, because your server does not send the file to the remote server, but the local file name (from the user). Also to transfer a file via CURL you need to add '@' in front of the name.

 $c_pic = '@'. $_FILES['c_pic']['tmp_name'] 

Then the remote script should also get the file from the $ _FILES variable and copy it somewhere with move_uploaded_file ( PHP.net )

Also tip: check $_FILES['c_pic']['error'] before doing this to prevent garbage from being downloaded to the remote server.

+2
source

Source: https://habr.com/ru/post/1481138/


All Articles