Sending multipart POST requests with PHP

php-logo

A long time ago I wrote about the same thing, but with more text and a C++ example, you can find it here. This time I made the same thing but in PHP and using cURL, because it was for a project I was working on. Basically I had a very simple task that needed to be a bit automated. Whole website is also basic, just simple authentication and then a simple file upload. In this project generate a lot of data (around 5mb) and then POST all of that to the file upload script.

For the uploading part I took the snippet from this website and changed it up. The original code was missing few eol‘s. This function is tuned for my needs, you will most likely have to change it up to fit your environment.
Here is the code I used:

public function uploadData($uploadData) {
    // echo '<pre>'; print_r($uploadData); die();
    $username = 'fancyuser';
    $password = 'simplepasswd';
    $loginUrl = 'http://some.website.com/login.php';
    $uploadUrl = 'http://some.website.com/upload.php';
 
    // here we log in and get a cookie
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $loginUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    // what to send to authenticate, use "Live HTTP Headers" for firefox or something to know what to send
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // make sure the dir where this script is located at is writeable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $store = curl_exec($ch); // redirection url
    if (strpos($store, 'url=upload.php') !== false) { // yeah log in was successful
        // curl_setopt($ch, CURLOPT_URL, $uploadUrl);
        // $content = curl_exec($ch);
        // file_put_contents('ish.txt', $store); die();
        // echo 'logged in<br />';
 
        // construct the upload body
        $eol = "\r\n";
        $data = '';
        $mime_boundary = md5(time());
        $data .= '--'.$mime_boundary.$eol;
        $data .= 'Content-Disposition: form-data; name="someformname"; filename="somefile.ext"'.$eol;
        $data .= 'Content-Type: application/octet-stream'.$eol.$eol;
        $data .= $uploadData.$eol;
        $data .= '--'.$mime_boundary.$eol;
        $data .= 'Content-Disposition: form-data; name="_action"'.$eol.$eol;
        $data .= 'upload'.$eol;
        $data .= '--'.$mime_boundary.'--'.$eol.$eol;
        // start to upload everything
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; boundary='.$mime_boundary));
        curl_setopt($ch, CURLOPT_URL, $uploadUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $response = curl_exec($ch);
        // echo $response; die();
        return $this->parseResponse($response);
    } else {
        return array('uploadInfo' => 'Failed to login', 'fileInfo' => 'Bad username or password');
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *