到目前为止,我想要一个 curl 命令将文件下载到特定目录中,所以我已经这样做了
system('curl -o hi.txt www.site.com/hi.html');
它不起作用,因为我的目录不可写,我需要找到一种方法来设置 curl 以将该文件下载到我的可写目录中。
您可以使用而不是 curlfile_get_contents
和file_put_contents
$file = file_get_contents('http://www.site.com/hi.html');
file_put_contents('/htdocs/mysite/images/hi.txt', $file);
即使未安装 curl 模块,此方法也可以使用。使用 cURL 来做到这一点(这将允许对实际的 http 请求进行更多控制):
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.site.com/hi.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$out = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
// Save the file
$fp = fopen('/htdocs/mysite/images/hi.txt', 'w');
fwrite($fp, $out);
fclose($fp);
system('curl -o /htdocs/mysite/images/hi.txt www.site.com/hi.html');