Last active
December 27, 2015 15:59
-
-
Save luffy-xiao/7352178 to your computer and use it in GitHub Desktop.
Besides uploading file with `@{file_name}` as `CURLOPT_POSTFIELDS` in curl, uploading multiple files with file content string is feasible.
The way is to build the POST headers manually.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// form field separator | |
$delimiter = '-------------' . uniqid(); | |
// file upload fields: name => array(type=>'mime/type',content=>'raw data') | |
$fileFields = array( | |
// For multiple files, name must be suffixed with '[]'. | |
'file[]' => array( | |
array( | |
'name' => 'file1.txt', | |
'type' => 'text/plain', | |
'content' => '...your raw file content goes here...' | |
), | |
array( | |
'name' => 'file2.txt', | |
'type' => 'text/plain', | |
'content' => '...your raw file content goes here...' | |
), | |
), | |
'file2' => array( | |
'name' => 'file2.txt', | |
'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"; | |
$data .= $content . "\r\n"; | |
} | |
// populate file fields | |
foreach ($fileFields as $name => $file) { | |
if (isset($file['content'])) | |
{ | |
$file = array($file); | |
} | |
foreach ($file as $f) | |
{ | |
$data .= "--" . $delimiter . "\r\n"; | |
// "filename" attribute is not essential; server-side scripts may use it | |
$data .= 'Content-Disposition: form-data; name="' . $name . '";' . | |
' filename="' . $f['name'] . '"' . "\r\n"; | |
// this is, again, informative only; good practice to include though | |
$data .= 'Content-Type: ' . $f['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 .= $f['content'] . "\r\n"; | |
} | |
} | |
// last delimiter | |
$data .= "--" . $delimiter . "--\r\n"; | |
$url = 'http://localhost/emails/server.php'; | |
$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); | |
$response = curl_exec($handle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Referred http://stackoverflow.com/questions/3085990/post-a-file-string-using-curl-in-php