Reading a file in PHP
This tutorial continues from PHP working with files " open file where we discussed the function ‘fopen()’ as well its parameters.
One more time I will describe ‘fopen()’ function. Here is the prototype of it:
![]() |
||||||||||||||||
|
|
Reading a file in PHP
Reading a file in PHP
This tutorial continues from PHP working with files " open file where we discussed the function ‘fopen()’ as well its parameters. One more time I will describe ‘fopen()’ function. Here is the prototype of it: $fp = fopen("$DOCUMENT_ROOT/../www/test.txt", 'w');When fopen() is called, it expects two, three, or four parameters. Usually, you use two,
as shown in this code line. The first parameter should be the file you want to open.You can specify a path to this file, as in the preceding code; The second fopen() parameter is the file mode, which should be a string.This string specifies what you want to do with the file. In this case, we are passing ‘w’ to fopen(); this means “open the file for writing.” There is a list of the file modes in the previous tutorial PHP working with files " open file. Now lets do something more with the file in the previous example. <?Before explaining the above example we must say that the files opened with ‘fopen()’ function must be closed! For that purpose we will use a function called ‘fclose()’, which will take as a parameter the pointer of the file we are closing.
The function ‘filesize()’ returns the file size in bits. If it happens to cannot be executed it will return ‘FALSE’. The file which size we want to know must be located on the local file system because this function is not working with URLs. When we know the size we can read the whole file with function ‘freed()’. It takes two arguments " the pointer of the file returned by ‘fopen()’ as well as the size of that part of information that we want to read. In this case we want to read the whole file so the second argument will be the returned by ‘filesize()’ value. Finally after we execute the above code on the server, the ‘$string’ variable will “read a file” and will contain the whole file content. Then the content will be printed on the screen. For example let say that the file contains the following: Line1Once the file is read, the assumed value inside the ‘$string’ variable will be: “Line1nLine2nLine3nLine4”. With “n” in PHP is defined as an Enter which means the content of the variable will be printed with a new line for each “n”.
Next Tutorials
Post Comment Related Tags:
openfilereadingwritngplacepointerbeginninggdtransparentpngwgetproxyinformationapacheserverstatuspagechangepaneappleerrordeletingfoldermakediskfullwriteprotectedsourcedestinationwindowsdeleteshareuploadfilezillaunlocksqlbasedagetaskruntimexlsxchooseprogramthumbsdbsystemremoveonlinegeneratecertificatepfximageisoneroburningromftpsinglelinezillacopydoscommandsexceldattypeawkscanningtextexportprivatekeyconvertssh2formatputtymergecrt |
Need a specific tutorial? Do not hesitate and submit a request! |
||||||||||||||
|
Home | Sitemap |
Terms & Privacy
© ONLINEHOWTO.net 2006-2010 |
||||||||||||||||