从站点到磁盘驱动器保存具有特定名称的文件

时间:2012-07-21 01:27:37

标签: php

这是我的代码,它没有完美运作。任何人都可以帮我确定问题吗?

$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

2 个答案:

答案 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();
}     

未经测试但应该可以正常使用。