Below is an example of how you can use HTTP PUT to transfer a local file a remote URL with PHP
The sending function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function putFile() { $url = 'http://my-receivingserver.com/receiver.php?' . http_build_query (array('filename' => 'the_fileneme','someParam' => 1)); $filePath = 'var/local/path/to/file.txt'; $file = fopen($filepath, 'rb'); $handle = curl_init($URL); curl_setopt($handle, CURLOPT_HEADER,0); curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($handle, CURLOPT_VERBOSE, 1); curl_setopt($handle, CURLOPT_PUT, 1); curl_setopt($handle, CURLOPT_INFILE, $file); $output = curl_exec($handle); curl_close($handle); fclose($file); } |
The receiver function
This piece of code will be used in order to handle the receiving file. As stated above this must be run inside “http://my-receivingserver.com/receiver.php”
1 2 3 4 5 6 7 8 9 10 11 |
$f = fopen( 'php://input', 'r' ); $out = ''; while( $line = fgets( $f ) ) { $out .= $line; } echo $out fclose( $f ); |