|
Free Educational Resource Center for teachers and students. Includes Interviews, Sourcecode, Free Software, Research Papers, Articles, Tutorials and much more.. |
R E S E A R C H A C T I V I T Y . C O M
Our Fellow Research Center for Ph.D Schollars |
|
| Home About Submit & Earn | Archives: C And C + + Programming » Dev Packages » Interviews » Php Mysql Programming » Windows Programming | |
|
Php Mysql Programming > reading and writing files in php
Reading and Writing files in PHP PHP follows almost same way of writing and manipulating files as in standard C. That is why it becomes so easy for a developer with C lanaguage background to create php application that uses files to save and retrieve data. It is a short tutoril that will get you started with writng and manipulating text files. To open a file either for writing, manipulating (adding new data, or appending new data at end of file), we use single function called fopen, but the second argument of the function changes accordingly. For example:
Here the second argument passed was "w" only, which means write the file, if it does not exists then create it, and if does not exist create new file. Now, if a file with name myfile.txt already exists, it will be rewritten and all of its bytes are lost, if it was not existing a new file with name myfile.txt is created. The second argument (we used "w") is the file opening mode, is dependant upon your requirement. In general disadvantage of using "w" as mode is if your ifle already exists, it is rewritten, and you loose all existing bytes. So it is recomended that you use mode "w" only when you need to create a new file or really wish to turn filesize to 0 (zero). Second mode is "r", which is using for reading and writing. Using this filemode you can open a file for reading and writing. Although modes "r" as well as (append "a") allows read and write but they do not affect the file while opening it, so the "w" also allows writing + reading but as mentioned, it rewrites the file. So, to open a file for reading and writing we will write:
And same if we need to open a file which starts writing data to the end of file (appends data), we will srite:
Remember : With both of these modes you can read as well as write data anywhere in the file. Now question appears, if I open a file with mode "r" now I have ended up reading soem data, how can I force my script to start writing data on the end of file ? Two answers: 1. If you have read entire data, the next writing automatically starts at the end of file. 2. If you read data less then the file size, you must be somewhere within file, and if you start writing data, the script will write data to the last position of data you ended up reading with. To obtain filesize we use a function filesize, to which we only pass name of file alongwith path (if not current directory) and it returns us the size of the file in bytes. e.g.
Now, when you have opened a file for reading and you wish to jump to a certain position (in this case end of file), you use a function fseek which lets you jump within file to any possible position. e.g. suppose your file size is 10 bytes, you read very first two characters. Now you wish to jum pto the end of file.
The same we will do when we need to jump to the start of file as: fseek($F,0); //0 means very first byte position Bytes position in a file have zero based indexes, 0 means 1st character, 9 means 10th character and so on. Now come to core of reading and wrting: Writing is very simple, as I have mentioned in all above talk the writing starts where pointer (position) currently is so the reading too. Example 1: Create new file and write string "My name is Ali" in it
In this example you see two new functions fwrite and fclose. fwrite is used to write something in a file at current position, since we created new file so writing starts at very first byte autoatically. And fclose is used to stop using a file anymore. This is necessary, becuase there is always a limit of opened files in operating system, so if your script is used much and leaves every file open without closing, it will results in no more file openable until system restart, either an http server (a web host) and/or local host. So, a new file is created and stirng "My name is Ali" is written in it (14 bytes written). Example 2: Read first 7 bytes from file we created in example 1 and print them to browser
In this example you encounter a ne function, called fread. This function is used to read specific number of bytes from a file. Its first argument is the file variable, and second is number of bytes to read. So we passed 7, becuase our rquirement at this time, is to read 7 bytes only. This function returned us the bytes read and we simpyl displayed them using print function, so it will display "My name" only the first7 bytes we read. Take a look at next example Example 3: Read all data from file we created in example 1 and print it to browser
In this example we used function filesize, which returned us the exact file size in bytes, we saved that size in a varible $size and then passed it to fread as secnd argument, so that the fread function reads all the data in file. I think it is simple to understand. It will display "My name is Ali" in the browser. Modifying data Example 4: Jump to byte # 12 and write string "Imran"
We opened file, jumped to byte number 12 using fseek function. Why used 11 when we need to start modifying from byte 12 ? Because I already mentioned that bytes in a file are accessed using zero based index, 11 means 12, 59 means 60, and 0 means 1st byte. Now run the example 3 and you will see the display in browser is "My name is Imran", which means we successfully have modified the file. Example 5: Add string ". My age is 30" to the end of file we created in example 1
We opened file with append mode, using "a" as second argumen to function fopen, which automatically takes position to the end of file, s owe dont need to use fseek in thsi regards. Now if you repeat running example 3 you will get the output as "My name is Imran. My age is 30". Extra things: 1. Another method to read entire file data in a variable
2. Read text file as an array of strings, where each line of text is an ite of array
3. If you dont have above function in your php distribution, use following ot read entire data
4. to delete a file
5. read page from internet pass url instead of fileme in ne of first 3 examples. I hope I have explained some. Let me know if you have any question. |