I was expecting that for each link found on the webpage the script could download a html file on my pc, asking me where to put the file(in which folder)... but instead the code download all the files on a folder on the server (the same where the php file is). I even try to erase the force download but nothing change.
What I do wrong? Thanks in advance.
this is my code:
$srcUrl= 'http://www.example.com/index.php';
$base= 'http://www.example.com/';
$html = file_get_contents($srcUrl);
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
//finding the a tag
$hrefs = $xpath->evaluate("/html/body//a");
header('Content-disposition: attachment; filename=' . $filename);
header("Content-Type: application/force-download");
header('Content-type: text/html');
//Loop to display all the links and download
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
//if real link
if($url!='#') {
//check if url is ok
$www= filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
if ($www == false )
{
//change the bad ones in absolute url;
$url2 = make_absolute($url, $base);
//Code to get the file...
$data = file_get_contents($url2);
//save as?
$filename = $url;
file_put_contents($filename, $data);
//a time to rest
sleep(5);
}
}
}
?>