PHP: How to use PUT method with CURL


Sample code:

<?php

set_time_limit(0);

$url = "http://yourserver.com/put.php";
$localfile = "1.mp4";
$fp = fopen($localfile, "r");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
print $http_code;

Remark lines 7,13 and 14.

And content file put.php

<?php

set_time_limit(0);
 
$f = fopen('php://input', 'r');

while ($line = fgets($f))
{
    file_put_contents("2.mp4", $line, FILE_APPEND);
}

fclose($f);

Wow, now you can upload file with PUT method.

1 Comment

Leave a Reply