heres the new version. has the option of overwriting existing files or numerating them.
i also made the output more colorful to help identify the activities of the script.
<?php
define("OVERWRITE_EXISTING", 0);
//if set to 1, files with the same name will be overwritten,
//if set to 0, the files will be numerated.
//no more config needed
error_reporting(E_PARSE | E_ERROR);
ini_set("track_errors", 1);
ini_set("max_execution_time", 0);
if($_POST) {
//IE OB Fix - Output 256 bytes before it will display the page and listen to flush().
echo "<!--\n";
for($i = 0; $i < 256; $i++) {
echo "x";
}
echo "\n-->\n\n";
$_POST['urls'] = str_replace("\r\n", "\n", $_POST['urls']);
$each = explode("\n", $_POST['urls']);
foreach($each as $url) {
$url = trim($url);
echo "<font style=\"color: #0000ff; font-weight: bold;\">Attempting to open $url</font><br />\n\n";
flush();
$data = @file_get_contents($url);
if($data === FALSE) {
echo "<font style=\"color: #ff0000; font-weight: bold;\">Could not retrieve data from $url. Reason: $php_errormsg</font><br /><br />\n\n";
flush();
continue;
}
$filename = strrchr($url, "/");
if($filename === FALSE || $filename == "") {
$host = strrchr($url, "://");
$host = str_replace("://", "", $host);
$host = str_replace("/", "_", $host);
$filename = "index_${host}.html";
}
$filename = str_replace("/", "", $filename);
if (file_exists($filename)) {
echo "<font style=\"color: #0000ff; font-style: italic;\">File $filename exists.</font><br />\n";
flush();
if (!OVERWRITE_EXISTING) {
echo "<font style=\"color: #0000ff; font-style: italic;\">Overwrite set to 0, finding next available filename.</font><br />\n";
flush();
$i = 1;
$fn = $filename;
$filename = $fn . ".$i";
while (file_exists($filename)) {
$filename = $fn . ".$i";
$i++;
}
echo "<font style=\"color: #0000ff; font-style: italic;\">Found next filename. Will use $filename.</font><br />\n";
flush();
} else {
echo "<font style=\"color: #ff0000; font-weight: bold;\">Overwrite set to 1, will overwrite $filename.</font><br />\n";
}
}
$fp = @fopen("./" . $filename, "w+");
if(!$fp) {
echo "<font style=\"color: #ff0000; font-weight: bold;\">Could not write data to file $filename. Reason: $php_errormsg.</font><br /><br />\n\n";
flush();
continue;
}
fwrite($fp, $data);
fclose($fp);
echo "<font style=\"color: #0000ff; font-weight: bold;\">Successfully wrote " . number_format(strlen($data)) . " bytes to $filename.</font><br /><br />\n\n";
flush();
}
} else {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Remote File Grabber - Written by Drew Phillips (drew-phillips.com)</title>
</head>
<body>
<font size=2><b>Remote File Grabber</b></font><br /><br />
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="urls" rows=15 cols=45></textarea><br /><br />
<input type="Submit" value="Retrieve Pages"><br />
<?php
}
?>