title: Upload text as file with POST (multipart/form-data) using php and wget
body: <p>I'm using PHP + wget to upload a file on an old cgi-bin control panel web-to-fax.</p>
  
  <p>The cgi (dont know its language) checks the file format (doc txt pdf) and then returns me the error: "incorrect file format". If I do it manually everything works fine.</p>
  
  <p>The form is 6 steps long, every step is quite similar (it asks you for destination, number of retries, etc) and my PHP + wget scripts work as expected (cookies handling, post, get, etc).</p>
  
  <p>This is how the form looks like (lightened and translated from the original):</p>
  
  <pre><code>&lt;FORM ACTION="xyz_httpfileupload" METHOD="post" ENCTYPE="multipart/form-data"&gt;
  Write the file with full path:
  
      &lt;INPUT TYPE="FILE" NAME="sourcefile" size=40 maxlength=200&gt;
      &lt;input type=hidden name=destfile value="N527yb"&gt;
      &lt;!-- this is a random generated filename --&gt;
  
      &lt;input type=hidden name=urlredirect value="./xyz_upload?destfile=N527yb&amp;filename=/tmp/VOTU9.txt"&gt;
      &lt;!-- this is another random generated filename --&gt;
  
      &lt;input type=submit name="Upload" value="Upload"&gt;
  &lt;/FORM&gt;
  </code></pre>
  
  <p>The code (and some tests) shows that you can use both GET or POST.</p>
  
  <p>This is my code, followed by explainations:</p>
  
  <pre><code>$command =  ' wget --debug -qSO- --cookies=on --keep-session-cookies '.
              ' --save-cookies='.$cookie.' --load-cookies='.$cookie.
              ' --header="Content-Type:text/plain" '.
              ' --post-file="'.$file.'" '.
  //          ' --post-data="sourcefile='.$rawfile.'" '.
              ' --post-data="'.
                  'destfile='.$randfile.
                  '&amp;urlredirect=xyz_delivery?destfile='.$randfile.
                  '&amp;filename=/tmp/'.$randname.'.txt" '.
                  '&amp;pload=Upload "xyz_httpfileupload" ';
  $data = shell_exec($command." &amp;&amp; sleep 10 2&gt;&amp;1 | tee ".$dir."logfile.txt");
  </code></pre>
  
  <p>$cookie is the full server path to the cookie file. It works as expected.</p>
  
  <p>$file is the full server path to my file. It's a simple file.txt</p>
  
  <p>$randfile and $randname are retrieved using preg_match on the HTML output.</p>
  
  <p>The commented line with $rawfile is a try to send the raw urlencoded content instead of the file: somebody say it's a working alternative but not for me, or at least I didn't code it right.</p>
  
  <p>I also tried to post directly into the URL you see in urlredirect, but I get the same error file format incorrect, but I can't assure the right parsing from the cgi if I jump a step. </p>
  
  <p>In any case logfile.txt was left empty.</p>
  
  <p>I know curl would make my life easier, but the ubuntu platform the script resides on only supports wget and I feel I'm half the way on.</p>
  
  <p>Advice me on methods to debug it. I can't via shell because I would lose the random names generated on every refresh. Perhaps TCPdump? Thanks for any advice you can give me, I think this is the ultimate discussion to definitely solve this matter.</p>