如何在curl中使用PUT方法上传文件到服务器


how upload file to server use method PUT in curl

我使用curl PUT在网站上传文件,但不明白为什么不工作

$url = "http://test.ru";
$localfile = "uploadTest.txt";
$fp = fopen ($localfile, "r");
$ch = curl_init();
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;
print "<br /><br />$http_result";
if ($error) {
    print "<br /><br />$error";
}

给出403错误。我尝试在apache.conf

中注册一种方式
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order Deny,Allow
    Deny from all
</Directory>
<Directory "%ssitedir%/*">
    AllowOverride All
    Options -MultiViews +Indexes +FollowSymLinks +IncludesNoExec +Includes +ExecCGI
<LimitExcept GET POST PUT HEAD>
    Order deny,allow
    Deny from all
</LimitExcept>
</Directory>
<Directory "%sprogdir%/modules/system/html/openserver">
    AllowOverride None
    Options -MultiViews -Indexes -FollowSymLinks -IncludesNoExec -Includes -ExecCGI
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/8 ::1/128
    Allow from %ips%
    %allow%Allow from all
    <LimitExcept GET POST PUT HEAD>
        Order deny,allow
        Deny from all
    </LimitExcept>
    AddDefaultCharset Off
    <IfModule dir_module>
    DirectoryIndex index.php
    </IfModule>
</Directory>
<Directory "%sprogdir%/modules/system/html/default">
    AllowOverride None
    Options -MultiViews -Indexes -FollowSymLinks -IncludesNoExec -Includes -ExecCGI
    Order deny,allow
    Allow from all
    <LimitExcept GET POST PUT HEAD>
        Order deny,allow
        Deny from all
    </LimitExcept>
    AddDefaultCharset Off
    <IfModule dir_module>
    DirectoryIndex index.html
    </IfModule>
</Directory>
<Directory "/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
</Directory>
<FilesMatch "^'.ht">
    Require all denied
</FilesMatch>
<IfModule alias_module>
    Alias /openserver/ "%sprogdir%/modules/system/html/openserver/"
</IfModule>

但是它给出了第405个错误哪里可以出错

Note:  both the following examples are from:
http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/ 
example 1:
$file_name_with_full_path = realpath('./sample.jpeg');
$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

example 2:
<?php
$target_url = 'http://127.0.0.1/accept.php';
    //This needs to be the full path to the file you want to send.
$file_name_with_full_path = realpath('./sample.jpeg');
    /* curl will accept an array here too.
     * Many examples I found showed a url-encoded string instead.
     * Take note that the 'key' in the array will be the key that shows up in the
     * $_FILES array of the accept script. and the at sign '@' is required before the
     * file name.
     */
$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
?>