Hướng dẫn append trong php

  • Trang chủ
  • Tham khảo
  • jQuery
  • jQuery - function
  • .append()

Định nghĩa và sử dụng

  • .append() Chèn nội dung, di chuyển thành phần vào trong thành phần khác, nội dung này thường được sắp xếp ở vị trí sau cùng.

Cấu trúc

  • Đã được thêm vào từ phiên bản 1.0

.append(nội dung)

$('div').append('

nội dung thêm vào

')
; $('div').append($('h3'));

  • Đã được thêm vào từ phiên bản 1.4

.append(nội dung)

Html viết:





Tiêu đề





Thành phần div

Hiển thị trình duyệt:

Ban đầu nội dung chỉ có thành phần div, nhưng khi sử dụng append thì thành phần div được chèn thêm thành phần

nội dung thêm vào

vào ngay vị trí sau cùng của thành phần div.

So sánh code HTML trước và sau khi có jQuery:

Trước khi có jQuerySau khi có jQuery

Thành phần div

Thành phần div

nội dung thêm vào


Ví dụ thêm

Html viết:





Tiêu đề





thành phần h3

thành phần p

Hiển thị trình duyệt:

Ban đầu thành phần h3 nằm trong thành phần div ở vị trí đầu, nhưng khi sử dụng append thì thành phần h3 được di chuyển ngay vị trí sau cùng của thành phần div.

So sánh code HTML trước và sau khi có jQuery:

Trước khi có jQuerySau khi có jQuery


thành phần h3


thành phần p



thành phần p


thành phần h3


.append(function(index){...})

Html viết:





Tiêu đề





thành phần div 01
thành phần div 02

Hiển thị trình duyệt:

Với cách sử dụng function như trên, ta đã thêm bên trong mỗi thành phần div lần lượt là thành phần p với nội dung được lấy từ tên class của thành phần div, các thành phần p này nằm ở vị trí sau cùng trong thành phần div tương ứng.

So sánh code HTML trước và sau khi có jQuery:

Trước khi có jQuerySau khi có jQuery

thành phần div 01

thành phần div 02

thành phần div 01

test01



thành phần div 02

test02


Mục lục nội dung

Nội dung chính

Nội dung chính

  • 1. Append file trong PHP
  • 2. Xóa file trong PHP
  • PHP Nối vào file – fwrite()
  • Understanding the file_put_contents() Function
  • Using file_put_contents() to Append Data to a File With PHP
  • Using fwrite() to Write Data to a File With PHP
  • Final Thoughts

  • 1. Append file trong PHP
  • 2. Xóa file trong PHP
  • PHP Nối vào file – fwrite()
  • Understanding the file_put_contents() Function
  • Using file_put_contents() to Append Data to a File With PHP
  • Using fwrite() to Write Data to a File With PHP
  • Final Thoughts

1. Append file trong PHP

Bạn có thể nối thêm dữ liệu vào file bằng cách sử dụng chế độ a hoặc a+ trong hàm fopen(). Hãy xem một ví dụ đơn giản nối thêm dữ liệu vào file data.txt.

Dữ liệu file data.txt ban đầu:

Ví dụ nối thêm dữ liệu vào file trong PHP, sử dụng hàm fwrite():

Dữ liệu file data.txt

Xin chào! PHP
Đây là dữ liệu được nối thêm vào file.

2. Xóa file trong PHP

Trong PHP, chúng ta có thể xóa bất kỳ file nào bằng cách sử dụng hàm unlink(). Hàm unlink() trong PHP chỉ chấp nhận một đối số: filename. Nó tương tự như hàm unlink() trong Unix.

PHP unlink() tạo ra lỗi mức E_WARNING nếu tệp không bị xóa. Nó trả về TRUE nếu tập tin bị xóa thành công nếu không FALSE.

Cú pháp:

bool unlink ( string $filename [, resource $context ])

$filename đại diện cho tên của file sẽ bị xóa.

Ví dụ xóa file trong PHP

Kết quả:

Trên đây là bài viết của eLib.VN về Append file và xóa file trong PHP. Chúng tôi hy vọng qua bài này sẽ làm tiền đề cho các bạn đam mê giải thuật tìm tòi thêm. Chúc các bạn thành công!

  • Tham khảo thêm

  • doc Xử lý file trong PHP
  • doc Mở file trong PHP
  • doc Đọc file trong PHP

Bạn có thể nối dữ liệu vào file bằng cách sử dụng chế độ a hoặc + trong hàm fopen(). Hãy xem một ví dụ đơn giản gắn dữ liệu vào file data.txt.

Chúng ta hãy xem dữ liệu của file trước.

data.txt

welcome to php file write

PHP Nối vào file – fwrite()

Hàm fwrite() trong PHP được sử dụng để ghi và nối dữ liệu vào file.

Thí dụ

  

Đầu ra: data.txt

welcome to php file write this is additional text appending data

Cài ứng dụng cafedev để dễ dàng cập nhật tin và học lập trình mọi lúc mọi nơi tại đây.

Tài liệu từ cafedev:

  • Full series tự học PHP từ cơ bản tới nâng cao tại đây nha.
  • Ebook về PHP tại đây.
  • Các nguồn kiến thức MIỄN PHÍ VÔ GIÁ từ cafedev tại đây

Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:

  • Group Facebook
  • Fanpage
  • Youtube
  • Instagram
  • Twitter
  • Linkedin
  • Pinterest
  • Trang chủ

Chào thân ái và quyết thắng!

Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!

Data is usually stored in a database when people are creating their website. However, sometimes we need to store data in files to make it easier for people to read or modify at a later time.

PHP comes with a lot of functions to read and write data to a file. We can also use a few of them to append data to a file. In this tutorial, you'll learn two different ways of appending data to a file with PHP.

Understanding the file_put_contents() Function

The file_put_contents() function is one of the easiest ways to write data to a file with PHP. It accepts four different parameters that determine its behavior. These parameters are:

  • filename: the path to the location of the file to which we want to write our data.
  • data: specifies the data that you want to write to the file. It is usually a string, but you can also specify an array or a stream resource. The function will automatically implode the contents of a single dimensional array with implode() in order to write the data to a file.
  • flags: controls the behavior of file_put_contents(). There are three different flags that you can set here, either by themselves or in combination with other flags. Different flags can be combined using the | operator.
  • context: useful only in providing additional data to PHP when you are reading or accessing content from a stream.

Using file_put_contents() to Append Data to a File With PHP

The default behavior of the file_put_contents() function is to overwrite the contents of a given file with any new data you provide. This is not desirable when you want to preserve the old data and add some new data. In such cases, you can use the FILE_APPEND flag to let PHP know that it should append data at the end of content originally present in the file.

Under some special circumstances, you might be appending data to a file from multiple scripts at the same time. In these situations, it is advisable to get an exclusive lock on the file using the LOCK_EX flag. This can help prevent data corruption or some other unexpected behavior. When you use this flag, other scripts will wait for the current process to complete writing to the file before they append their own data.

Here is an example in which some text is appended to an existing file using file_put_contents().

In the above example, we wrote some strings to a file called canada.txt which contains information about Canada. Both the string were appended at the end of the file one after the other.

Keep in mind that this function will create a file if one doesn't already exist. However, it won't create a non-existent directory. So it might be a good idea to check if a file exists before you start writing to it.

Using fwrite() to Write Data to a File With PHP

Using the file_put_contents() function to write data to a file with PHP is similar to calling fopen(), fwrite(), and fclose() in that order. This means that doing multiple write operations on the same file can be inefficient because we are constantly opening and closing the file again and again.

One way to overcome this issue is to call these functions yourself. Just begin by calling fopen() at the start of the write operation. After that, write content to the file as many times as you like with the fwrite() function. In the end, you can simply call fclose() to close the file handle. Let's discuss each of these steps in detail now.

The fopen() function accepts four different parameters that you can use to tell PHP how it should open a file.

  • filename: the name of the file that you want to open.
  • mode: the mode for opening a file can be specified using either one or two characters. We want to open the file and then append some text to it. To append, set the mode with the character a or a+. This will place the file pointer at the end of the file. PHP will also try to create the file if it doesn't already exist. When files are opened with the a+ mode, you can also read the contents of the file.
  • use_include_path: instructs PHP to look for files inside the specified include path as well. Defaults to false.
  • context: useful only in providing additional data to PHP when you are reading or accessing content from a stream.

Now that the file is open, we can use the fwrite() function to add information to the file. fwrite() takes three parameters:

  • resource: this is the resource handle we created earlier with fopen().
  • string: the text that you want to append to your file.
  • length: is optional and it is used to set the maximum number of bytes that should be written to the file.

You can close the file handle by using the fclose() function once you have completed all your write operations.

Here is an example that shows you how to use fopen(), fwrite(), and fclose() to append data to a file.

Contents of square.txt

Square of 1 is: 1.
Square of 2 is: 4.
Square of 3 is: 9.
Square of 4 is: 16.
Square of 5 is: 25.
Square of 6 is: 36.
Square of 7 is: 49.
Square of 8 is: 64.
Square of 9 is: 81.
Square of 10 is: 100.

In this case, we are writing the square of the numbers 1 to 10 in a file called square.txt. We opened it using the fopen() function in a+ mode, which means we can also read content from the file as well as appending our own content. A new line with the current value of $i and its square is appended to our file with each iteration of the for loop.

There are a few functions like fread() and fgets() that you could use to read whatever is written inside the file. However, you will usually need to use fseek() to place the file pointer at the desired position to read the data as expected. After the loop, we go to the beginning of the file and read its first line with fgets().

Finally, we close the handle to our file by calling the function fclose().

Final Thoughts

In this tutorial, we learned two different methods of appending data to a file with PHP. Using the file_put_contents() function is more convenient for writing data to a file. However, using fwrite() can be more efficient when you have to do multiple write operations on a file. Opening a file with fopen() to append the data also gives you the option to read its contents by moving the file pointer to the desired location.

Did you find this post useful?

Freelancer, Instructor

I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around.