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; } |