我正在尝试对文件执行 CURL PUT,但我遇到了问题。
这是我的代码:
$url_path_str = 'http://my_url';
$file_path_str = '/my_file_path';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
$file_data_str = fread($fh_res, filesize($file_path_str));
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
fclose($fh_res);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
脚本一直超时,我不确定为什么。
我会很感激一些帮助。谢谢。
最佳答案
我想通了。恰好是 fclose 导致了这个问题。我只是把它放在 curl_exec 之后。
修改后的代码:
$url_path_str = 'http://my_url';
$file_path_str = '/my_file_path';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
fclose($fh_res);
希望这对以后的其他人有帮助。
干杯。
关于来自文件路径的 PHP CURL PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7340809/