这是我的代码,它没有完美运作。任何人都可以帮我确定问题吗?
$urls = file('list.txt', FILE_IGNORE_NEW_LINES);
foreach($urls as $url) {
copy(trim($url),"c:/data/$url");
echo "$url is done";
ob_flush();
flush();
}
某些网址不存在。
我希望URL中的每个文件都使用URL的名称保存。
网址如下:http://site.com/index.htm
答案 0 :(得分:0)
$urls = file('list.txt', FILE_IGNORE_NEW_LINES);
foreach($urls as $url) {
$con=file_get_contents($url);
if($con !== false)
{
if(file_put_contents(trim($url),$con))
{
echo "$url is done";
//don't really see the point of these, but okay...
ob_flush();
flush();
}
}
}
应该这样做。
答案 1 :(得分:0)
这是PHP cURL函数:
/* gets the data from a URL */
function get_urlData($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
在现有的foreach
之前将它放在PHP文件中。
您的代码已经改编:
$urls = file('list.txt', FILE_IGNORE_NEW_LINES);
foreach($urls as $url) {
$data = get_urlData($url);
copy($data, "c:/data/$url");
echo "$url is done";
ob_flush();
flush();
}
未经测试但应该可以正常使用。