Create file in php example

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.

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:

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.

Читайте также:  Css gradient красивый пример

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

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.

Источник

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.

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.

Источник

Creating a file in PHP With Examples

Creating a file in PHP

In this article, we will be creating a file in PHP in the easiest way with a detailed explanation as well as best program examples. This article is a continuation of the previous topic, entitled PHP File to Array.

Creating a file in PHP With Examples

In PHP, creating a file can simply be done with the use of fopen() function.

Creating a file using the fopen() function in PHP

The fopen() is a built-in function which is used for creating a file . It sounds a little bit confusing, but in PHP (Hypertext Preprocessor). The same function can create a file and open a files .

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

For creating a new file with the use of fopen() function we need to specify or declared a $filename and one mode.

TAKE NOTE: If you want to create a ( binary file ), you will need to append the character ‘ b ‘ to an argument name $mode . For example, this ‘ wb +’ automatically opens and close the file for writing a binary.

 foreach ($numbers as $number) < fputs($f, $number); >fclose($f); ?>

The program works in this way.

  • First, is to define an array which consists of five numbers from 11 to 15.
  • Second, the fopen() and fwrite functions should be used to create a numbers.dat file.
  • Third, the fputs() function is used to write each number in the array name $numbers to file.
  • Lastly, terminate the file with the use of fclose() function.

Creating a file using the file_put_contents() function in PHP

The file_put_contents() is a function which is used to write data into the existing file.

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

TAKE NOTE: Once the file upload is showing results of $filename doesn’t exist, the function successfully creates the file.

In addition, if the function returns file_put_contents() is identical when calling the fputs() , fopen() , and fclose() functions sequentially to write data in a file.

  • First, the program will download a webpage https://itsourcecode.com/ with the use of function file_get_contents() functions.
  • Lastly, the program will write the HTML (HyperText Markup Language) file using the function file_put_contents() .

What does file () do in PHP?

The file() simply works by reading a file into an array. Each element on the array contains a number of lines from the file handling where the newline character is still attached.

How to create a folder and save it in PHP?

In PHP, to create a folder and save, it can simply be done by the function name mkdir() . This is a built-in function that creates a new folder and directory with a specified pathname.

Furthermore, the path and mode are sent as (parameters) to the function mkdir() and it will return TRUE if successful otherwise FALSE if fails.

What is file and directory in PHP?

The file and directory is a set of functions used to retrieve details, update them, and fetch or get information on various system file directories in PHP.

How do I run a PHP file?

The PHP file located inside the ( htdocs ) folder, if you want to execute or run the system, you need to open it in any web browser such as Google Chrome, Mozilla Firefox, and Brave and enter the folder name example “localhost/sample. php” and simply click the enter button.

How do I edit a PHP file?

To edit a file you will need to install a text editor such as VS Code , Sublime Text and any other PHP code editor online.

What is the file extension of PHP?

The file extension of PHP is ( .php ) which need to be put on the end of every file which is similar to a PowerPoint file with a (.ppt) file extension.

Summary

This article tackle in Creating a file, and also tackles Creating a file using the file_put_contents() function, what does file () does, how to create a folder and save it, what is file and directory, how we run a PHP file, how do I edit a PHP file, and what is the file extension.

I hope this lesson has helped you learn PHP a lot. Check out my previous and latest articles for more life-changing tutorials that could help you a lot.

Leave a Comment Cancel reply

You must be logged in to post a comment.

Источник

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