Write HTML Pages To new Folder With PHP

Creating HTML Web Pages From PHP Files With PHP

This tutorial below explains how you can grab a php file on the Internet and rewrite and save it as an html file using php.

Note: This tutorial must be used in a file that exists in the same directory. For example, this example grabs all website urls like page1.php, page2.php that exists in the tablename table. Then, it grabs the html content of each url that has a file within the the same folder; such as example.com/myfolder/page1.php. After the contents for the web page is grabbed, it writes a new file to a folder called html. Thefore, the original file example.com/myfolder/page1.php actually becomes example.com/myfolder/html/page1.php.

Alternatively, you could just have made a hard coded array for the urls; like
$urls = array('example.com/myfolder/page1.php', example.com/myfolder/page2.php'');

## FUNCTION TO GRAB URL
function my_page_URL() {

$page_URL = 'http';
if ($_SERVER["HTTPS"] == "on") {$page_URL .= "s";}
$page_URL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$page_URL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$page_URL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $page_URL;
}

## GET WEB PAGE
## We use curl to get the url because file_get_contents() function will interpret example.com/myfile.php different than myfile.php
## if the absolute url is not grabbed and the relative url is grabbed, file_get_contents() will grab the actual file code and not ## what ouputs in a browser.
$mycurl=my_page_URL();

       ##GET URLS
       $command = "SELECT urls FROM tablename ";
       $result = mysqli_query($db, $command);  
       while($row = mysqli_fetch_assoc($result)){
       $url = $row['url'];
       $urls[] = $url;
		}	

           ##LOOP EACH PAGE		
	   foreach($urls as $url){
	   //echo $url;
	   # we don't want the file downloads.php so we skip it from making a page called downloads.html
	   if($url == "downloads.php"){continue;}
	   echo $url;
	   $myurl = str_replace("make_html.php",$url,$mycurl);
	   $htmla = file_get_contents($myurl);
	   echo $htmla;
	   $html = str_replace(".php",".html",$url);
	   // echo $html;
	   $file = "html/".$html;
	   $fp = fopen($file, 'w');
	   fwrite($fp, $htmla);
	   fclose($fp);
	   }

Example #2 Grabbing Any Web Page and Making It HTML

## FUNCTION TO GRAB URL

function my_page_URL() {

$page_URL = 'http';
if ($_SERVER["HTTPS"] == "on") {$page_URL .= "s";}
$page_URL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$page_URL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$page_URL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $page_URL;
}
## This tutorial below explains how you can grab a php file on the Internet and rewrite and save it as an html file using php. 

##GET WEB PAGE
## We use curl to get the url because file_get_contents() function will interpret example.com/myfile.php different than myfile.php
## if the absolute url is not grabbed and the relative url is grabbed, file_get_contents() will grab the actual file code and not ## what ouputs in a browser.
$mycurl=my_page_URL();

$urls = array('http://example.com/learn-web-design.php','http://example.com/services.php');		

##LOOP EACH PAGE		
	   foreach($urls as $url){
	   //echo $url;
	   # we don't want the file downloads.php so we skip it from making a page called downloads.html
	   if($url == "downloads.php"){continue;}
	   echo $url;
	   //$myurl = str_replace("write_new_file.php",$url,$mycurl);
	   $htmla = file_get_contents($url);
	   echo $htmla;
	   
	   // echo $html;
	   $html = str_replace("http://example.com/","",$url);
	   $html = str_replace(".php",".html",$html);
	   $file = "html/".$html;
	   $fp = fopen($file, 'w');
	   fwrite($fp, $htmla);
	   fclose($fp);
	   }