Đang chuẩn bị liên kết để tải về tài liệu:
PHP and MySQL Web Development - P21

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

PHP and MySQL Web Development - P21: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | File Locking 67 customer calls fopen and also begins writing What will be the final contents of the file Will it be the first order followed by the second order or vice versa Will it be one order or the other Or will it be something less useful like the two orders interleaved somehow The answer depends on your operating system but is often impossible to know. To avoid problems like this you can use file locking. This is implemented in PHP using the flock function.This function should be called after a file has been opened but before any data is read from or written to the file. The prototype for flock is bool flock int fp int operation int wouldblock You need to pass it a pointer to an open file and a number representing the kind of lock you require. It returns true if the lock was successfully acquired and false if it was not. The possible values of operation are shown in Table 2.2.The possible values changed at PHP 4.0.1. Both sets of values are shown in the table. Table 2.2 flock Operation Values Value of operation Meaning LOCK_SH formerly 1 Reading lock.This means the file can be shared with other readers. LOCK_EX formerly 2 Writing lock. This is exclusive. The file cannot be shared. LOCK_UN formerly 3 LOCK_NB formerly 4 Release existing lock. Adding 4 to the operation prevents blocking while trying to acquire a lock. If you are going to use flock you will need to add it to all the scripts that use the file otherwise it is worthless. Note that flock does not work with NFS or other networked file systems. It also does not work with older file systems that do not support locking such as FAT. On some operating systems it is implemented at the process level and will not work correctly if you are using a multithreaded server API. To use it with this example you can alter processorder.php as follows fp fopen DOCUMENT_ROOT . orders orders.txt a flock fp LOCK_EX lock the file for writing fwrite fp outputstring flock fp LOCK_UN release write lock fclose fp You should .