php - Sending a file to a webservice with XML using cURL corrupts it -


so have code sends career application webservice emails file hr department attachment along other elements in form. of parts except file gets emailed desired. file gets uploaded server without being corrupted. resulting email attachment ends being corrupted.

the problem is; file ends abruptly before reaching eof. let's pdf file when open both original , reduced size file in text editor see beginnings identical until 1 of them ends. 1 of them 1mb , corrupt 1 600kb.

i have tried sending files smaller(4kb) resulting corrupt file file gets corrupt in same way. resulting file 1kb.

the xml response says:

<?xml version="1.0" encoding="utf-8"?><sendemlrsp><rtcd>1</rtcd><exp>ok</exp><rsp_list><rsp><msgid>0</msgid><eid /><result>invalid length base-64 char array or string.</result></rsp></rsp_list></sendemlrsp> 

it part of interest:

<result>invalid length base-64 char array or string.</result> 

i have prepared small form file upload testing purposes.

here html:

<html>     <body>      <form action="upload_file.php" method="post" enctype="multipart/form-data">     <label for="file">filename:</label>     <input type="file" name="file" id="file"><br>     <input type="submit" name="submit" value="submit">     </form>      </body> </html> 

here relevant php code:

if(isset($_files['file']['name']))               {             echo ($_files['file']['name']);             echo ($_files['file']['tmp_name']);             $target = $_files['file']['name'];               move_uploaded_file( $_files['file']['tmp_name'], $target);              $rawdata = file_get_contents($target);              $data = urldecode($rawdata);             $data = base64_encode($rawdata);               //error_log('uploadconvertscope');               $iletisimrcpt = '<rcpt>                            <ta>someemail@address.com</ta>                            <msg>kgsg</msg>                            <sbj>'. strlen($rawdata).'</sbj>                            <oboe>'.oboe.'</oboe>                            <obon>'.obon.'</obon>                            <att_list><att><fn>'.$_files['file']['name'].'</fn><data>'.$data.'</data></att></att_list>                   </rcpt>';             $request = '<?xml version="1.0" encoding="utf-8"?>                <sendeml>                    <version>1.0</version>                    <token>'.$token.'</token>                    <jid>'.jobid.'</jid>                    <msg>kariyar basvuru isteði baþarýyla yerleþtirildi.</msg>                    <sbj>kariyar basvuru</sbj>                    <rcpt_list>                    '.$iletisimrcpt.'                    </rcpt_list>                </sendeml>';         error_log($request );         $params = array('data' => $request);          $response = processrequest(eml_url, $params);          error_log($response );         $xml = new simplexmlelement($response);         } 

the processrequest function works rest of message. may not problem here code:

<?php          function processrequest($url, $params) {          if(!is_array($params))          return false;          $post_params = "";          foreach($params $key => $val) {          $post_params .= $post_params?"&":"";          $post_params .= $key."=".$val;          }          $ch = curl_init();          curl_setopt($ch, curlopt_ssl_verifypeer, 0);          curl_setopt($ch, curlopt_url, $url);          curl_setopt($ch, curlopt_post, 1);          curl_setopt($ch, curlopt_returntransfer, 1);          curl_setopt($ch, curlopt_verbose, 0);          curl_setopt($ch, curlopt_timeout, 0);          curl_setopt($ch, curlopt_header, false); // 'true', developer testing purpose          curl_setopt($ch, curlopt_customrequest, 'post');          curl_setopt($ch, curlopt_postfields, $post_params);          $data = curl_exec($ch);          if(curl_errno($ch))          print curl_error($ch);          else          curl_close($ch);          return $data;          }          ?> 

the file when read string between looks ahfay3453gaw//long random string of characters//== ends 2 "==" signs if means anything.

i stumped files uploaded ok c# code:

        byte[]  attach1 = file.readallbytes(@"c:\users\user\downloads\amb.pdf");         string attach = convert.tobase64string(attach1);         emlrequest.setconnectioninformation("someapi.com", "admin", "password");         emlrequest eml=new emlrequest(){  messagejobid="dasfa1sdfawefa4x2==" };         eml.recipients.add(new apiemlrecipient() { targetaddress = "email@address.com" ,toname="name",message="xxx",subject="subject"});         eml.recipients[0].attachments.add(new apiemlattachment() { filename = "abm.pdf", data = attach });         eml.send(); 

which identical it's php version.

i figured out quite time ago got down writing answer.

so problem "+" signs in string replaced spaces.

this bit responsible:

$rawdata = file_get_contents($target); $data = urldecode($rawdata); $data = base64_encode($rawdata); 

i changed this:

$rawdata = file_get_contents($_files['uploadedfile']['tmp_name']); $data = base64_encode($rawdata); $data = urlencode($data); 

now works.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -