Skip to main content

POST a file string using cURL in PHP?



I was wondering if it is possible to post a file - along with other form data - when the file is just a string?





I know that you can post a file that is already on the filesystem by prefixing the filepath with "@".





However I'd like to bypass creating a temporary file and send just the file as a string, but I am unsure how to construct the request using cURL in PHP.





Cheers







$postFields = array(

'otherFields' => 'Yes'

,'filename' => 'my_file.csv'

,'data' => 'comma seperated content'

);



$options = array(

CURLOPT_RETURNTRANSFER => true

,CURLOPT_SSL_VERIFYPEER => false

,CURLOPT_SSL_VERIFYHOST => 1

,CURLOPT_POSTFIELDS => $postFields

,CURLOPT_HTTPHEADER => array(

'Content-type: multipart/form-data'

)

);





Source: Tips4all

Comments

  1. Should be possible: here's a form, posted through a browser (irrelevant fields omitted):

    POST http://host.example.com/somewhere HTTP/1.1
    Content-Type: multipart/form-data; boundary=---------------------------7da16b2e4026c
    Content-Length: 105732

    -----------------------------7da16b2e4026c
    Content-Disposition: form-data; name="NewFile"; filename="test.jpg"
    Content-Type: image/jpeg

    (...raw JPEG data here...)
    -----------------------------7da16b2e4026c
    Content-Disposition: form-data; name="otherformfield"

    content of otherformfield is this text
    -----------------------------7da16b2e4026c--


    So, if we build the POST body ourselves and set an extra header or two, we should be able to simulate this:

    // form field separator
    $delimiter = '-------------' . uniqid();
    // file upload fields: name => array(type=>'mime/type',content=>'raw data')
    $fileFields = array(
    'file1' => array(
    'type' => 'text/plain',
    'content' => '...your raw file content goes here...'
    ), /* ... */
    );
    // all other fields (not file upload): name => value
    $postFields = array(
    'otherformfield' => 'content of otherformfield is this text',
    /* ... */
    );

    $data = '';

    // populate normal fields first (simpler)
    foreach ($postFields as $name => $content) {
    $data .= $delimiter . "\r\n"
    $data .= 'Content-Disposition: form-data; name="' . $name . '"';
    // note: double endline
    $data .= "\r\n\r\n";
    }
    // populate file fields
    foreach ($fileFields as $name => $file) {
    $data .= "--" . $delimiter . "\r\n"
    // "filename" attribute is not essential; server-side scripts may use it
    $data .= 'Content-Disposition: form-data; name="' . $name . '";' .
    ' filename="' . $name . '"' . "\r\n";
    // this is, again, informative only; good practice to include though
    $data .= 'Content-Type: ' . $file['type'] . "\r\n";
    // this endline must be here to indicate end of headers
    $data .= "\r\n";
    // the file itself (note: there's no encoding of any kind)
    $data .= $file['content'] . "\r\n";
    }
    // last delimiter
    $data .= "--" . $delimiter . "--\r\n";

    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle, CURLOPT_HTTPHEADER => array(
    'Content-Type: multipart/form-data; boundary=' . $delimiter,
    'Content-Length: ' . strlen($data);
    );
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    curl_exec($handle);


    This way, we're doing all the heavy lifting ourselves, and trusting cURL not to mangle it.

    ReplyDelete
  2. php has access to a temporary location "php://memory", which actually makes what you're trying to do fairly easy.

    $fh = fopen('php://memory','rw');
    fwrite( $fh, $content);
    rewind($fh);

    $options = array(
    CURLOPT_RETURNTRANSFER => true
    ,CURLOPT_SSL_VERIFYPEER => false
    ,CURLOPT_SSL_VERIFYHOST => 1
    ,CURLOPT_HTTPHEADER => array(
    'Content-type: multipart/form-data'
    )
    ,CURLOPT_INFILE => $fh
    ,CURLOPT_INFILESIZE => strlen($content)
    );

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex