Create directories and write files, friends in need can refer to it.
Requirement description:
Generate a static HTML page from the previewed PHP page and store it in the corresponding folder.
1. Preview information is to query the database and transfer the corresponding information to the view layer for display.
2. Generate PHP page into HTML
- $root_dir = $_SERVER["DOCUMENT_ROOT"]; //Get the root directory
- $NowYear = date("Y"); //Create a directory with a layer of "year"
- $dir_path = "$root_dir/abc/ bcd/".$NowYear."/".$edm['showtime']."";//Get a directory based on the current year and the corresponding time
- $file_path = $dir_path."/index.html"; // The file name is index.html
-
-
- mkdir($dir_path,0777,true);//Creating a file directory 777 is the permission, true means that multi-level directories can be generated
-
- $edm_templates = $sqlquery_get_data;//SQL gets data
- $ data = array( //Some data needed for the page
- 'info' => $info,
- 'result' => $edm_templates,
- );
-
- $this->load->vars ( 'info' , $data );
- $data = $this->load->view ( URL,'',true); //Get the data needed to generate HTML
- if(empty($data)) ajaxReturn('' ,'No read permission or the corresponding file was not found! ',0);
- file_put_contents($file_path,$data);//Generate file
- ajaxReturn('','Generated successfully!',2);
Copy code
The previous idea was to use the file_get_contents() function to obtain page information, but the page requires login. That is, the server requests the page and is blocked on the login page. Previously, this function was used to generate the Login page.
In general, creating a directory is the mkdir function. When writing files, it is more convenient to use the file_put_contents function.
|