我使用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个错误 可能出现错误的地方?
答案 0 :(得分:0)
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;
?>