TAILIEUCHUNG - Giải pháp thiết kế web động với PHP - p 22

USING PHP TO MANAGE FILES The while loop uses fgets() to retrieve the contents of the file one line at a time—!feof($file) is the same as saying until the end of $file—and stores them in $contents. Both methods are more long-winded than file() or file_get_contents(). However, you need to use either fread() or fgets() if you want to read and write to a file at the same time. Replacing content with fopen() The first of the write-only modes (w) deletes any existing content in a file, so it s useful for working with files that need to be updated frequently. You can test. | USING PHP TO MANAGE FILES The while loop uses fgets to retrieve the contents of the file one line at a time feof file is the same as saying until the end of file and stores them in contents. Both methods are more long-winded than file or file_get_contents . However you need to use either fread or fgets if you want to read and write to a file at the same time. Replacing content with fopen The first of the write-only modes w deletes any existing content in a file so it s useful for working with files that need to be updated frequently. You can test the w mode with which has the following PHP code above the DOCTYPE declaration php if the form has been submitted process the input text if isset _POST contents open the file in write-only mode file fopen C private w write the contents fwrite file _POST contents close the file fclose file There s no need to use a loop this time you re just writing the value of contents to the opened file. The function fwrite takes two arguments the reference to the file and whatever you want to write to it. In other books or scripts on the Internet you may come across fputs instead of fwrite . The two functions are identical fputs is a synonym for fwrite . If you load into a browser type something into the text area and click Write to file PHP creates and inserts whatever you typed into the text area. Since this is just a demonstration I ve omitted any checks to make sure that the file was successfully written. Open to verify that your text has been inserted. Now type something different into the text area and submit the form again. The original content is deleted from and replaced with the new text. The deleted text is gone forever. Appending content with fopen The append mode is one of the most useful ways of using fopen because it adds new content at the end preserving any existing content. The main code in is the same as .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.