Php file creation methods

Source: Internet
Author: User
Tags php file

Create a php file

The code is as follows: Copy code

<? Php
$ Str = "<? Php echo 123;?> ";
File_put_contents ('test. Php', $ str); // use a script to create a php file
?>

Example 2

The code is as follows: Copy code

<? Php
If ($ argc! = 2 ){
Die ("Usage: php mkphp. php filename ");
}
Array_shift ($ argv );
$ Cat = $ argv [0];
File_put_contents ($ cat. ". php", "<? Php

?> ");

Use fopen to create a file

The code is as follows: Copy code

<?

$ Fp = fopen ("1.txt"," w + "); // for other fopen () switches, see related functions.
$ Str = "Add me + add ";
Fputs ($ fp, $ str );
Fclose ($ fp );
?>

If you do not have any concerns above, first, determine the directory permission for the file to be created. We recommend that you set the device to 777. We recommend that you use an absolute path for the new file name.

The code is as follows: Copy code

<? Php
$ Filename = "test.txt ";
$ Fp = fopen ("$ filename", "w +"); // open the file pointer and create a file
If (! Is_writable ($ filename )){
Die ("File:". $ filename. "cannot be written. Please check it! ");
}
// Fwrite ($ filename, "anything you want to write to $ filename .";
Fclose ($ fp); // Close the pointer

'R' indicates that the file is read-only, and the file refers to the start point.
'R + ': the file can be read and written, and the file refers to the start point.
The 'W' method is to write the file. The file refers to the start point and the length of the original 'is set to 0. If the file does not exist, ''create a new file-
'W + ': the file can be read and written. The file refers to the start point and the length of the original' is set to 0. If the file does not exist, ''create a new file-
'A file is written in the open file mode, and the object refers to the end of the file. If the file does not exist, ''create a new file-
'A + 'indicates whether the file is read/write, and the object refers to the end of the file. If the file does not exist, ''create a new file-
'B' if the operating system's text and binary files are different, 'can be used. UNIX systems do not-"use parameters ".

The code is as follows: Copy code

/// Create a file
Function creat_file ($ PATH ){
$ SFile = "test.html ";
If (file_exists ($ PATH. $ sFile )){
Creat_file ();
} Else {
$ Fp = fopen ($ PATH. $ sFile, "w ");
Fclose ($ fp );
   }
Return $ sFile;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.