note 39868 modified in function.fsockopen by danbrown

From: Date: Sun, 20 Jul 2008 19:50:48 +0000
Subject: note 39868 modified in function.fsockopen by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142316@lists.php.net to get a copy of this message
<?php // This is a modification to the script I submitted below. // This script is an example of posting multiple files using // 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. $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); ?> --was-- <? // This is a modification to the script I submitted below. // This script is an example of posting multiple files using // 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. $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); ?> http://php.net/manual/en/function.fsockopen.php

« previous php.notes (#142316) next »