Skip to content

Instantly share code, notes, and snippets.

@Gowee
Created November 12, 2015 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gowee/ef89985418bc176580d5 to your computer and use it in GitHub Desktop.
Save Gowee/ef89985418bc176580d5 to your computer and use it in GitHub Desktop.
alternative for file_get_contents("php://input");
<?php
function getRawPostData(){
$eol = "\r\n";
$BOUNDARY = substr(ltrim(explode(";", $_SERVER["CONTENT_TYPE"])[1]), strlen("boundary="));
$postData = "";
foreach($_POST as $name => $value){
$postData .= "--" . $BOUNDARY . $eol;
$postData .= 'Content-Disposition: form-data; name="' . $name . '"' . $eol . $eol;
$postData .= $value . $eol;
}
foreach($_FILES as $name => $info){
if(is_array($info['tmp_name'])){//name[]
foreach($info['tmp_name'] as $index => $tmpName)
{
if(!empty($info['error'][$index]) and $info['error'][$index] !== UPLOAD_ERR_NO_FILE/*4*/)
{
continue;
}
if(empty($tmpName) or is_uploaded_file($tmpName)){
$postData .= "--" . $BOUNDARY . $eol;
$postData .= 'Content-Disposition: form-data; name="' . $name . '[]"; filename="' . $info['name'][$index] . '"' . $eol ;
$postData .= "Content-Type: application/octet-stream" . $eol . $eol;
$postData .= (empty($tmpName) ? "" : file_get_contents($tmpName)) . $eol;
}
}
}
else{
if(empty($info['tmp_name']) or is_uploaded_file($info['tmp_name'])){
$postData .= "--" . $BOUNDARY . $eol;
$postData .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $info['name'] . '"' . $eol ;
$postData .= (empty($info['tmp_name']) ? "" : "Content-Type: " . (empty($info['type']) ? "application/octet-stream" : $info['type'])) . $eol . $eol;
$postData .= (empty($info['tmp_name']) ? "" : file_get_contents($info['tmp_name'])) . $eol;
}
}
}
$postData .= '--' . $BOUNDARY . '--' . $eol . $eol;
return $postData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment