PHP Tutorial: File System
Building the Right Environment to Support AI, Machine Learning and Deep Learning
To access a file you first have to establish a connection to that file. This is done by opening the file with the fopen function. To use the fopen function you provide it two parameters. The first is the name of the file you wish to use and the second says how you want to use the file (for reading or writing, etc.) Using the fopen function establishes a pointer to the file. Here's an example:
$myfilepointer = fopen("myfilename.txt", "w");
|r+||reading and writing|
|w||write only & create the file if it doesn't exist|
|w+||reading and writing & create the file if it doesn't exist|
|a||writing only, create if it doesn't exist, and place a file position pointer at the end of the file (appends records to an existing file)|
|a+||reading and writing, create if it doesn't exist and
place file position pointer at the end of the file
These parameters have introduced the idea of a file position pointer. As you read a file, something has to keep track of where you are in the file so that you can read the next record. That something is the file position pointer. This pointer gets interesting when you think about writing to the file. If you are writing to a new file it's pretty straight forward -- you write a record, the pointer points at the end of that record and you write another record, and so on. If, however, the file already exists and contains some records, the value of the pointer when you first open the file becomes very important! If the file is opened with the pointer at the beginning of the file (like r+, w or w+), when you write to that file you will overwrite the existing records in the file, replacing them with your new records. On the other hand, if you open it with the pointer pointing at the end of the file (like a or a+), you will append records to the file, adding them to the existing records.
Reading and Writing Records
Now that we have our file open, we need to read and/or write records. This is accomplished with the fgets and fputs functions.
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...