fopen() Function The fopen() function opens a specified file or URL for reading and/or writing. The syntax of fopen() is fopen("filename", "mode") To open a URL, use http:// or ftp:// at the beginning of the file name string. If the file name begins with anything else, then the file is opened from the filesystem and a file pointer to the opened file is returned. Otherwise, the file is assumed to reside on the local filesystem. The specified mode determines whether the file is opened for reading, writing, or both. r Read-only. The file pointer is at the beginning of the file. r+ Reading and writing. The file pointer is at the beginning of the file. w Write-only. The file pointer is at the beginning of the file, and the file is truncated to zero length. If the file does not exist, attempt to create it. w+ Reading and writing. The file pointer is at the beginning of the file, and the file is truncated to zero length. If the file does not exist, attempt to create it. a Write-only. The file pointer is at the end of the file (it appends content to the file). If the file does not exist, attempt to create it. a+ Reading and writing. The file pointer is at the end of the file (it appends content to the file). If the file does not exist, attempt to create it. For example, to open the file index.html in your home directory for reading only, use $fp = fopen("/home/username/index.html", "r"); To open a nonexistent file called temp.txt in your home directory, use $fp = fopen("/home/username/temp.txt", "w+");