0

In below code using i try to upload multiple files to remote URL.
but only 1 file data only i am receiving. please help any one to resolve issue.
Remotepage.php i am not getting sign,cheque file details

<?php 
function do_post_request($url, $postdata, $files = null) 
{ 
    $data = ""; 
    $boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10); 
       
    //Collect Postdata 
    foreach($postdata as $key => $val) 
    { 
        $data .= "--$boundary\n"; 
        $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n"; 
    } 
     
    $data .= "--$boundary\n"; 
    
    //Collect Filedata 
    foreach($files as $key => $file) 
    { 
        $fileContents = file_get_contents($file['tmp_name']); 
        
        $data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n"; 
        $data .= "Content-Type: image/jpeg\n"; 
        $data .= "Content-Transfer-Encoding: binary\n\n"; 
        $data .= $fileContents."\n"; 
        $data .= "--$boundary--\n"; 
    } 
  
    $params = array('http' => array( 
           'method' => 'POST', 
           'header' => 'Content-Type: multipart/form-data; boundary='.$boundary, 
           'content' => $data 
        )); 

   $ctx = stream_context_create($params); 
   $fp = fopen($url, 'rb', false, $ctx); 
   
   if (!$fp) { 
      throw new Exception("Problem with $url, $php_errormsg"); 
   } 
  
   $response = @stream_get_contents($fp); 
   if ($response === false) { 
      throw new Exception("Problem reading data from $url, $php_errormsg"); 
   } 
   return $response; 
} 

//set data (in this example from post) 

//sample data 
$postdata = array( 
    'name' => $_POST['name'], 
    'age' => $_POST['age'], 
   ); 

//sample image 
$files['photo'] = $_FILES['txtphoto'];
$files['sign'] = $_FILES['txtsign'];
$files['cheque'] = $_FILES['txtcheque'];


do_post_request("http://example.com/remotepage.php", $postdata, $files); 




//remotepage.php code

$somecontent = $_POST['name'];
$somecontent.= $_POST['age'];
$somecontent.= $_FILES['photo']['name'];
$somecontent.= $_FILES['sign']['name'];
$somecontent.= $_FILES['cheque']['name'];

$ourFileName ='./uploads/a.txt';
$ourFileHandle = fopen($ourFileName,'w');
if (fwrite($ourFileHandle, $somecontent) === FALSE) {
        echo "Cannot write to file ($ourFileName)";
        exit;
   }
fclose($ourFileHandle);

?>
2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by albivl91
0

i changed $data .= "--$boundary--\n"; to $data .= "--$boundary\n";

it solved it

0

hey everyone who is seeing this article please need help with the above code to make it work on linux server (php 5.2.*)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.