-1

Im not sure if its possible or not but Im looking for a PHP script to copy an entire website so that I can save it on my local server and view it when internet is not at my disposal So pretty much something like

 <?php    
function copyDir($source,$dest) {    
if(is_file($source)) {    
$cpy= copy($source, $dest);    
return $cpy;    
}    
if(!is_dir($dest)) {     
mkdir ($dest); }     
$dir= dir($source);    
while (false !== $entry = $dir->read()) {     
 if ($entry == "." || $entry == "..") {    
continue; }    
if ($dest !== "$source/$entry") {       
copyDir("$source/$entry", "$dest/$entry");    
}     
}    
$dir->close();    
return true;    
} ?>    

But to copy a website instead of a directory.... I hope that makes sense

  • 3
    "an" entire website? or "your" entire website. The methods you are showing here are all for your local filesystem and would therefor never work for external sites. If it is purely local, then sure you can.. Though in that case one would wonder why you would do that with php... – Damien Overeem Aug 19 '14 at 07:02
  • I am still learning web development and Don't always have internet available. Like I said I am still learning and testing, sometimes I get stuck and have to go back and look at w3schools or php.net to refresh my memory on codes. so I want to copy those sites so I can check them offline ... – user3903151 Aug 19 '14 at 07:18
  • If w3schools is your base source of information, you are in trouble :) But if you just need a few websites locally, there are heaps of programs that can download them for you. (try googling something like "Website downloader"). PHP is definatly not what you want to use to just download a website. – Damien Overeem Aug 19 '14 at 07:19
  • I figured php would most likely be the easiest way to do this – user3903151 Aug 19 '14 at 07:21

2 Answers2

0

What you're asking is not trivial. You need to send a request to the site in question download the source to a file, then you want to parse the DOM for all images, scripts and stylesheets, and download all of them, while keeping the directory structure.

It has nothing to do with the function you've presented, which only copies files locally.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • We cannot make that on all websites they may have not given permission in all cases. If you do try with the AJAX then browser itself doesnt support CORS – Jenno Richi Benat Aug 19 '14 at 07:23
  • @LifeHacker the question is about PHP, not JavaScript. PHP does not respect CORS. Just in case you were confused, DOM is not JavaScript exclusive. – Madara's Ghost Aug 19 '14 at 07:23
0

As stated above, you will need to parse all of the DOM and also go looking for links too and then parse all of those.

And, even if you manage that, note that some of the content you download may have been generated dynamically or depend on remote sources. Assuming you have the site owner's permission to do this, why not just ask for the files as a ZIP or tarball?

KevInSol
  • 2,560
  • 4
  • 32
  • 46