PHP File create/write Example

PHP create file and write file

In this PHP tutorial we will learn how to create a file and how to write to a file on the server.

  • Create a File – touch() ,
  • Create a File, if does not exist – fopen()
  • Write to a File – fwrite()

PHP – Create a File

The touch() function is used to create a file on server. The fopen() function is also used to create a file in PHP. If we use fopen() function on a file the file does not exist, then it will create it. The touch() function and fopen() work same for creating a file on server where the php file directory code.

The PHP touch() function Syntax :

PHP touch() function example

Here we use PHP touch() function to create a new file called ‘abc.txt’, ‘abc.doc’ and ‘abc.pdf’ on server.

When run above php code the file abc.txt, abc.pdf and abc.doc will be created in PHP program folder.

PHP fopen() function

When we use fopen() function for creating a new file, must assign mode character with function (w) for writing mode or (a) for appending mode.

fopen() syntax :

Example of fopen() function

The fopen() function has following modes :

  • w = write only. if file exist open it, if not then create a new file.
  • w+ = read/write
  • r = read only
  • r += read / write
  • a = append file
  • a+ = read / append
  • x = write only. create new file, returns error if file already exists.
  • x+ = read/write.

PHP fwrite() Function

The PHP fwrite() function is used to write to a file means write contents inside the file.

Читайте также:  METANIT.COM

PHP fwrite() syntax

The fwrite() function has first parameter is file name and the second parameter is text to be written in file.

fwrite() function example

For write context to file we must use fopen() function to open the file and then use fwrite() function for write to file.

The output is : when we open abc.txt file it has content like : “Meera Academy”. Here, we first open the file abc.txt and write some text using fwrite() function and then finally close the file using fclose() function.

PHP form example of fwrite()

     Enter String :  

?>

Источник

PHP File Create/Write

In this chapter we will teach you how to create and write to a file on the server.

PHP Create File — fopen()

The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files.

If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

The example below creates a new file called «testfile.txt». The file will be created in the same directory where the PHP code resides:

Example

PHP File Permissions

If you are having errors when trying to get this code to run, check that you have granted your PHP file access to write information to the hard drive.

PHP Write to File — fwrite()

The fwrite() function is used to write to a file.

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

The example below writes a couple of names into a new file called «newfile.txt»:

Example

$myfile = fopen(«newfile.txt», «w») or die(«Unable to open file!»);
$txt = «John Doe\n»;
fwrite($myfile, $txt);
$txt = «Jane Doe\n»;
fwrite($myfile, $txt);
fclose($myfile);
?>

Notice that we wrote to the file «newfile.txt» twice. Each time we wrote to the file we sent the string $txt that first contained «John Doe» and second contained «Jane Doe». After we finished writing, we closed the file using the fclose() function.

Читайте также:  Python события нажатия клавиш

If we open the «newfile.txt» file it would look like this:

PHP Overwriting

Now that «newfile.txt» contains some data we can show what happens when we open an existing file for writing. All the existing data will be ERASED and we start with an empty file.

In the example below we open our existing file «newfile.txt», and write some new data into it:

Example

$myfile = fopen(«newfile.txt», «w») or die(«Unable to open file!»);
$txt = «Mickey Mouse\n»;
fwrite($myfile, $txt);
$txt = «Minnie Mouse\n»;
fwrite($myfile, $txt);
fclose($myfile);
?>

If we now open the «newfile.txt» file, both John and Jane have vanished, and only the data we just wrote is present:

PHP Append Text

You can append data to a file by using the «a» mode. The «a» mode appends text to the end of the file, while the «w» mode overrides (and erases) the old content of the file.

In the example below we open our existing file «newfile.txt», and append some text to it:

Example

$myfile = fopen(«newfile.txt», «a») or die(«Unable to open file!»);
$txt = «Donald Duck\n»;
fwrite($myfile, $txt);
$txt = «Goofy Goof\n»;
fwrite($myfile, $txt);
fclose($myfile);
?>

If we now open the «newfile.txt» file, we will see that Donald Duck and Goofy Goof is appended to the end of the file:

Complete PHP Filesystem Reference

For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.

Источник

PHP Create File

Summary: in this tutorial, you will learn a couple of ways to create a new file in PHP.

Creating a file using the fopen() function

The fopen() function opens a file. It also creates a file if the file doesn’t exist. Here’s the syntax of the fopen() function:

fopen ( string $filename , string $mode , bool $use_include_path = false , resource $context = ? ) : resourceCode language: PHP (php)

To create a new file using the fopen() function, you specify the $filename and one of the following modes:

Mode File Pointer
‘w+’ At the beginning of the file
‘a’ At the end of the file
‘a+’ At the end of the file
‘x’ At the beginning of the file
‘x+’ At the beginning of the file
‘c’ At the beginning of the file
‘c+’ At the beginning of the file
Читайте также:  Мобильные приложения java это

Except for the ‘a’ and ‘a+’ , the file pointer is placed at the beginning of the file.

If you want to create a binary file, you can append the character ‘b’ to the $mode argument. For example, the ‘wb+’ opens a binary file for writing.

The following example uses fopen() to create a new binary file and write some numbers to it:

 $numbers = [1, 2, 3, 4, 5]; $filename = 'numbers.dat'; $f = fopen($filename, 'wb'); if (!$f) < die('Error creating the file ' . $filename); > foreach ($numbers as $number) < fputs($f, $number); >fclose($f); Code language: HTML, XML (xml)
  • First, define an array of five numbers from 1 to 5.
  • Second, use the fopen() to create the numbers.dat file.
  • Third, use the fputs() function to write each number in the $numbers array to the file.
  • Finally, close the file using the fclose() function.

Creating a file using the file_put_contents() function

The file_put_contents() function writes data into a file. Here’s the syntax of the file_put_contents() function:

file_put_contents ( string $filename , mixed $data , int $flags = 0 , resource $context = ? ) : intCode language: PHP (php)

If the file specified by the $filename doesn’t exist, the function creates the file.

The file_put_contents() function is identical to calling the fopen() , fputs() , and fclose() functions successively to write data to a file.

The following example downloads a webpage using the file_get_contents() function and write HTML to a file:

 $url = 'https://www.php.net'; $html = file_get_contents($url); file_put_contents('home.html', $html);Code language: HTML, XML (xml)
  • First, download a webpage https://www.php.net using the file_get_contents() function.
  • Second, write the HTML to the home.html file using the file_put_contents() function

Summary

  • Use the fopen() function with one of the mode w , w+ , a , a+ , x , x+ , c , c+ to create a new file.
  • Use the file_put_contents() function to create a file and write data to it.
  • The file_put_contents() function is identical to calling fopen() , fputs() , and fclose() functions successively to write data to a file.

Источник

Оцените статью