Web hosting reseller - PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY the

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY the existing content. At other times, you may want to append new material. At yet other times, you may want PHP to create a file if it doesn t already exist. The other thing you need to understand is where each mode places the internal pointer when it opens the file. It s like the cursor in a word processor: PHP starts reading or writing from wherever the pointer happens to be when you call fread() or fwrite(). Table 7-2 brings order to the confusion. Table 7-2. Read/write modes used with fopen() Type Mode Description Read-only r Internal pointer initially placed at beginning of file. Write-only w Existing data deleted before writing. Creates a file if it doesn t already exist. a Append mode. New data added at end of file. Creates a file if it doesn t already exist. x Creates a file only if it doesn t already exist, so no danger of deleting existing data. Read/write r+ Read/write operations can take place in either order and begin wherever the internal pointer is at the time. Pointer initially placed at beginning of file. File must already exist for operation to succeed. w+ Existing data deleted. Data can be read back after writing. Creates a file if it doesn t already exist. a+ Opens a file ready to add new data at end of file. Also permits data to be read back after internal pointer has been moved. Creates a file if it doesn t already exist. x+ Creates a new file, but fails if a file of the same name already exists. Data can be read back after writing. Choose the wrong mode, and you could end up overwriting or deleting valuable data. You also need to be careful about the position of the internal pointer. If the pointer is at the end of the file, and you try to read the contents, you ll end up with nothing. On the other hand, if the pointer is at the beginning of the file, and you start writing, you ll overwrite the equivalent amount of any existing data. You work with fopen() by passing it the following two arguments: The pathname to the file you want to open One of the modes listed in Table 7-2 (for a binary file, such as an image, add b)
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Leave a Reply