Read Time:8 minsLanguages:url-selector#selectionChanged" data-controller="url-selector">EnglishDeutschEspañolIn this tutorial, we are going to learn file handling in PHP, I"ll show you how to create, read, write, and delete files in PHP by using the built-in file handling functions" /> Read Time:8 minsLanguages:url-selector#selectionChanged" data-controller="url-selector">EnglishDeutschEspañolIn this tutorial, we are going to learn file handling in PHP, I"ll show you how to create, read, write, and delete files in PHP by using the built-in file handling functions" />

HOW TO CREATE, WRITE, READ, AND DELETE FILES IN PHP

post-promotion-placement#checkSidebarAdDisplayCondition" data-controller="sticky-ad post-promotion-placement ">
Read Time:8 minsLanguages:url-selector#selectionChanged" data-controller="url-selector">EnglishDeutschEspañol

In this tutorial, we are going to learn tệp tin handling in PHP. I"ll show you how khổng lồ create, read, write, và delete files in PHP by using the built-in file handling functions.

Bạn đang xem: How to create, write, read, and delete files in php

File handling is something that you will need to vì very often as a PHP developer.

You could use PHP file handling functions to manipulate files in different ways. These functions could be used lớn build features in your applications that range from custom error logging to storing cached files. Examples of utility tools that you could build with these functions are:

custom logging and debugging toolsapplication configuration storagefront-end & application cachinglocalization supportand many more

PHP provides several tệp tin handling functions that allow you khổng lồ perform various operations, like:

create and xuất hiện a filewrite into a fileread from a filedelete a fileclose a file

Today, we’ll go through each & every file operation, along with examples of how to use them. I would encourage you to lớn try out the examples in this tutorial as you follow along, so that you can actually learn and understand how they work. Và if something doesn’t work as expected for you, feel free to post your queries by using the feed at the over of this tutorial.

How to Create and mở cửa a File

In this section, we’ll see how to create and xuất hiện a file.

When it comes to lớn creating a file, it’s the fopen function which you"ll over up using most of the time. It may seem a bit confusing lớn use the fopen function to create a file. In fact, the fopen function does two things: it creates a tệp tin if it doesn"t exist and also opens it for reading or writing.

Let’s go through the following example to understand how it works.

In the above example, the fopen function will check if the /home/obatambeienwasirherbal.com/files/tmp.txt file exists, & if it exists, it’ll open it for writing. By supplying "w" in the second argument, we specify that we will be writing to lớn the file. If the file doesn’t exist, it’ll be created right away. It’s important to lưu ý here that the /home/obatambeienwasirherbal.com/files/ directory in the above example must be writable by the web vps user for the fopen function to be able to lớn create a file.

The first argument of the fopen function is the filename which you want to lớn open. In the above example, we’ve supplied the /home/obatambeienwasirherbal.com/files/tmp.txt filename in the first argument. Again, it’s important to chú ý that we’ve supplied an absolute path name.

The second argument is the mode, which specifies the type of access you require to lớn the opened file. The fopen function provides different modes you can choose from. For example:

use the r mode to open a file for readingthe r+ mode for both reading và writinga mode for reading và appending

In our example, we’ve used the w mode, which opens the /home/obatambeienwasirherbal.com/files/tmp.txt tệp tin for writing only. Feel không tính tiền to go through the official documentation khổng lồ see different modes supported by the fopen function.

fopen returns a file system pointer, which is used for the other tệp tin functions like reading & writing.

How Can I xuất hiện Remote Files?

The fopen function isn"t just for local files. It also supports other protocols & can mở cửa files from elsewhere on your network or the web. If you’ve enabled the allow_url_fopen directive in PHP, you could mở cửa remote files as well.

It’s really important to note that when you enable the allow_url_fopen directive, you are creating some security risks, since it opens the door for remote file execution & other attacks. So make sure you take extra security measures in your application if you’re going to lớn enable this directive.

How to Write to lớn a tệp tin With PHP

There are a couple of different ways you can write khổng lồ a tệp tin with PHP.

The fwrite Function

First và foremost is the fwrite function, which allows you khổng lồ write string contents khổng lồ the file stream referenced by the file handle. Let’s go through the following example to understand how it works.

First, we’ve opened the /home/obatambeienwasirherbal.com/files/tmp.txt file with the a+ mode, which opens it for reading và writing, with the tệp tin pointer placed at the end of the file. Thus, our content will be appended lớn the end of the file, after any other contents. Next, we’ve used the fwrite function to write a string. 

The first argument of the fwrite function is the file system pointer returned by fopen—this is how fwrite knows where lớn write into. And the second argument is a string which we want lớn write into a file. As you can see in the above example, you can use the fwrite function multiple times lớn write a series of strings before you close the file.

Xem thêm: Tải Game Join Clash 3D (Mod Vô Hạn Tiền) 2, Tải Game Join Clash 3D

Finally, we’ve used the fclose function to lớn close the file. It takes only one argument, the tệp tin pointer that you want lớn close. It’s always a good practice khổng lồ close files by using the fclose function once you’ve finished with your file operations.

The file_put_contents Function: A PHP Shortcut for Writing to lớn Files

In the previous section, we discussed the fwrite function, which is used lớn write into a file. You would have noticed that, if you want khổng lồ write into a file, you need to mở cửa it with the fopen function in the first place. After that, you need lớn use the fwrite function khổng lồ write your data into a file, và finally you need to use the fclose function khổng lồ close the file.

If that sounds too much khổng lồ you, there’s a shortcut: file_put_contents. The file_put_contents function allows you to write data lớn a file in a single call.

Let’s see how it works.

The first argument of the file_put_contents function is a filename, & the second argument is a string which you want to lớn write into a file. If the tệp tin doesn’t exist, it’ll be created.

As you can see, the file_put_contents function is a shortcut when you just want lớn write a piece of data to a file.

How to lớn Read From a tệp tin in PHP

Now you know how to create và write into a file. In this section, I"ll show you how to read from a file.

The fread Function

The fread function allows you khổng lồ read from a file. It"s similar lớn fwrite, but you need khổng lồ provide the length in bytes you want to read.

Let’s have a look at the following example to understand how it works.

As we want to lớn read from the /home/obatambeienwasirherbal.com/files/tmp.txt file, we’ve opened it with the r mode. Next, we’ve used the fread function to read all the contents of the file into the $content variable.

The first argument of the fread function is the file system pointer, so that it knows where lớn read from. The second argument is the length in bytes you want khổng lồ read from a file. In our case, we want khổng lồ read all the contents of the /home/obatambeienwasirherbal.com/files/tmp.txt file, và thus we’ve used the filesize function lớn measure the kích cỡ of the file.

The file_get_contents Function: A PHP Shortcut for Reading From Files

In the previous section, we discussed the fread function, which allows you to lớn read a tệp tin by specifying the length in bytes you want to lớn read. If you want lớn read an entire tệp tin at once, there"s a function which allows you to vì chưng just that: file_get_contents.

Let’s see the file_get_contents function in action!

As you can see, it’s pretty straightforward to lớn use the file_get_contents function—just provide a filename in the first argument.

How to lớn Delete a tệp tin in PHP

In this last section, we’ll see how you can delete files. To lớn delete a file in PHP, use the unlink function. Let’s go through an example lớn see how it works.

The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE, depending on whether the delete operation was successful.

Conclusion

In this post, we discussed the basics of tệp tin handling in PHP. Throughout the article, we discussed different operations you can perform by using tệp tin functions in PHP. If you have any queries or suggestions, feel free to post them using the feed below!

Learn PHP With a không tính tiền Online Course

If you want to learn PHP, check out our free online course on PHP fundamentals!