Home > Backend Development > PHP Problem > How to create php file

How to create php file

(*-*)浩
Release: 2023-02-24 22:20:01
Original
5827 people have browsed it

PHP Create file - fopen()

How to create php file

##fopen () function is also used to create files. Maybe it's a little confusing, but in PHP, the functions used to create files are the same as those used to open files.

If you use fopen() to open a file that does not exist, this function creates the file, assuming the file is opened for writing (w) or adding (a). (Recommended learning: PHP Programming from entry to proficiency)

The following example creates a new file named "testfile.txt". This file will be created in the same directory as the PHP code:

Instance

$myfile = fopen("testfile.txt", "w")
Copy after login

PHP File Permissions

If An error occurred while you were trying to run this code. Please check if you have PHP file access to write information to your hard drive.

PHP Write File - fwrite()

fwrite() function is used to write files.

The first parameter of fwrite() contains the file name of the file to be written, and the second parameter is the string to be written.

The following example writes the name to a new file named "newfile.txt":

Example

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Copy after login

Please note that we The file "newfile.txt" is written twice. Each time we write to the file, the string $txt we send contains "Bill Gates" the first time and "Steve Jobs" the second time. After writing is complete, we use the fclose() function to close the file.

If we open the "newfile.txt" file, it should look like this:

Bill Gates
Steve Jobs
Copy after login

The above is the detailed content of How to create php file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template