Home >Backend Development >PHP Tutorial >Create directories and write files

Create directories and write files

WBOY
WBOYOriginal
2016-07-25 08:46:201228browse

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

  1. $root_dir = $_SERVER["DOCUMENT_ROOT"]; //Get the root directory
  2. $NowYear = date("Y"); //Create a directory with a layer of "year"
  3. $dir_path = "$root_dir/abc/ bcd/".$NowYear."/".$edm['showtime']."";//Get a directory based on the current year and the corresponding time
  4. $file_path = $dir_path."/index.html"; // The file name is index.html
  5. mkdir($dir_path,0777,true);//Creating a file directory 777 is the permission, true means that multi-level directories can be generated
  6. $edm_templates = $sqlquery_get_data;//SQL gets data
  7. $ data = array( //Some data needed for the page
  8. 'info' => $info,
  9. 'result' => $edm_templates,
  10. );
  11. $this->load->vars ( 'info' , $data );
  12. $data = $this->load->view ( URL,'',true); //Get the data needed to generate HTML
  13. if(empty($data)) ajaxReturn('' ,'No read permission or the corresponding file was not found! ',0);
  14. file_put_contents($file_path,$data);//Generate file
  15. 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.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn