Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 868501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:09:00+00:00 2026-05-15T10:09:00+00:00

I was wondering if it is possible to post a file – along with

  • 0

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'
        )
    );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:09:00+00:00Added an answer on May 15, 2026 at 10:09 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if it is possible to post data without using the standard
I'm wondering if its possible to add new class data members at run-time in
I was wondering if it is possible to generate a core file, copy if
Possible Duplicate: Render a view as a string Hi I am wondering is it
I was just wondering if there was a possible way to add a custom
I'm wondering if its possible to resize flash with javascript. I can't change any
I'm wondering if its possible to do this. The issue being that im trying
I was wondering if its possible to inject a thread into a remote app
I am wondering if it is possible to extract the index position in a
I was wondering if it is possible to not attach Excel sheet if it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.