Skip to content

Instantly share code, notes, and snippets.

@cyc0der
Created November 7, 2013 20:48
Show Gist options
  • Save cyc0der/7361617 to your computer and use it in GitHub Desktop.
Save cyc0der/7361617 to your computer and use it in GitHub Desktop.
This script is an example of posting multiple files using (PUT method) fsockopen. The tricky part is making sure the HTTP headers and file boundaries are acceptable to the target webserver. This script is for example purposes only and could/should be improved upon.
<?php
$host='targethost';
$port=80;
$path='/test/socket/file_upload/receive_files.php';
// the file you want to upload
$file_array[0] = "dingoboy.gif"; // the file
$file_array[1] = "dingoboy2.gif"; // the file
$file_array[2] = "dingoboy3.gif"; // the file
$content_type = "image/gif"; // the file mime type
//$content_type = "text/plain";
//echo "file_array[0]:$file_array[0]<br><br>";
srand((double)microtime()*1000000);
$boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);
$data = "--$boundary";
for($i=0;$i<count($file_array);$i++){
$content_file = join("", file($file_array[$i]));
$data.="
Content-Disposition: form-data; name=\"file".($i+1)."\"; filename=\"$file_array[$i]\"
Content-Type: $content_type
$content_file
--$boundary";
}
$data.="--\r\n\r\n";
$msg =
"POST $path HTTP/1.0
Content-Type: multipart/form-data; boundary=$boundary
Content-Length: ".strlen($data)."\r\n\r\n";
$result="";
// open the connection
$f = fsockopen($host, $port);
fputs($f,$msg.$data);
// get the response
while (!feof($f)) $result .= fread($f,32000);
fclose($f);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment