The motto: Reading and writing files is one of the only things our programmers do on an empty night when there is no database in the year (perhaps not necessarily oh ~ ~ ~). So even now that this technology has become simple, fast and ordinary, we still need to maintain a careful attitude and strict integrity, do not neglect its importance and rigor. Text start: About reading a file I have a little story. Early studio start, it is business panic, QQ on a friend said he has a friend unit to do website, introduced to me to do. I am very excited, immediately tidy hair cut off cigarette butts, because my friend said that the customer and so on to my video (so tide? )。 It is also said that he is a large enterprise IT director, very formal, to my attention, not too decadent. I shaved the electric shaver out of the bag. I was very fond of this thousands of ocean website business, basically is the row row interface to do the CSS, not more than 100 PHP code. Send a novice one weeks to fix can, although reusability and follow-up business development is not, but one months of alcohol and tobacco conductress must have got. Of course, I have to talk about, let the novice to talk, I basically can only eat in the big stalls at night. The video opened, opposite appeared a handsome man, the appearance is tactful, the vocal cords are very fine. Each other very false courtesy, he proudly introduced a bit of his factory, very large, because of the development of too fast too fast too quickly too fast, and now want to do a website (I do not know the scale is too fast and want to do the site has too much contact) to fit the scale, finally sent me an Excel, has painted the homepage structure diagram, Before the company's small software is he did, because he recently too busy too busy too busy too busy, so this time want to find a network company developed, words at the end handsome man tactful told me, he technical aspects "very understand oh ~". A gust of cold wind Blows ~ ~ ~. I point to open Excel, I admire the handsome man's "sketch" Foundation, can excel in the page structure drawing out and still can draw so the image is rare. I agree, and said to be able to deliver on time, and I have sent the Excel to my art and programmer, let him do, the time is 3 days. But then the handsome men asked me to almost want to cover the desktop display, his request is to put the picture and text directly in Excel, and then in the Excel built multiple workbooks (I have been blindfolded, do not know whether to use $sheet or shit to express "Workbook"), home only a PHP page, according to his settings to read the workbook in Excel. This can be done every time the user opens the homepage is different. In order to show the multi-angle of their plant business "diversification" and innovation. Handsome man in the video proud smile let me very much on my own on the "configurable, product" design ideas have great doubts. I want to reject this alien-like thinking, but the handsome man insisted on his "idea", he said that his design has been his director of the full "high evaluation." Because of the charm of the handsome male "transfer cheque", I succumbed to his "configurable" mentality. I just want to say, he is too good, I can not do large enterprise IT director, because I really can not think of this approach to implement the CMS concept of template switching. Of course I am not a fool (reading Office files is not very simple), I used a very simple way to solve the problem. and successfully got a cheque for the transfer. The method is very simple, with the HTML basic page design five different handsome male requirements of the page, and then save as 5 different Excel (Note: Although the saved file suffix is xls, but in fact, HTML file), with PHP to read the file display. The code was as follows: Here are a few points of knowledge: 1, the RAND function takes a random number, the parameter is the range rand (minimum value, maximum value). In the example, a number is randomly generated from 1 to 5. Then piece together a file name. such as Index1.xls,index2.xls,index3.xls,index4.xls,index5.xls, represents me for the handsome man designed 5 pages saved 5 xls files. Now that we've talked about the RAND function, we have to expand the knowledge Point: Array_rand function, (array--array We've been spitting on the last day, and here we're not going to go into it) the way you can randomly cobble together files $file =array (" Index1.xls "," Index2.xls "," Index3.xls "," Index4.xls "," Index5.xls "); $fileName = ' index '. $file [Array_rand ($file, 1)]. ' XLS '; This means that Array_rand returns the key value of the passed-in array, while the second parameter represents a return of several. If it is 1, a character or number variable is returned, and if more than 1, an array is returned, which contains 2 random key values. 2, file_get_contents function This function is very important, we must remember, is also a very common use of a file content reading function. It represents a single time to read the contents of the file, and read the array. Remember: once. You can't interfere with it in the process of reading. You want it to read halfway to stop and do something else, two words "no". Remember: The return must be a string. such as $getFileContent =file_get_contents ("Index1.xls"); This shows that the handsome man asked me to meet. The prototype of this function is: file_get_contents (path,include_path,context,start,max_length) path: The path of the file, remember that the path is calculated from the current page where your "code" is finally executed, "here". /"represents the current directory,". /"On behalf of the previous level of the directory," http://www.cnblogs.com/http://www.cnblogs.com/http://www.cnblogs.com/"This represents the N-level directory (Trouble yourself to count it) Include_ Path: A lot of textbooks and online articles with a sentence if you want to use Include_path to fill 1. The others are gone. I think in this era of cottage rampant, we copied too much. Actually include_pAth is an environment variable that you can set with Set_include_path, or you can use Get_include_path to get it. What is the role of it is actually very simple, include_path is you set up a bunch of folder path, for reference or index convenience. In the above example, your website is deployed in d:/web/, there is a index.php, theoretically your index1. Index5.xls will also be placed in the d:/web/, so that the read only needs to use file_get_contents ("Index1.xls"). Yes, but suppose the handsome man must put you in the other directory is not placed in the site directory, such as c:/handsome man's BT file/ , we must not write file_get_contents ("c:/handsome man's bt file/index1.xls"); The reason is simple: in the pure program in the appearance of "handsome man" three words is how wretched, and then a future web site to be migrated to Linux will not run up. The correct way is to modify php.ini include_path =.: c:/The handsome man's BT file:./other folders. Migrate to Linux to change the configuration file. In this case the above program can still be written file_get_contents ("Index1.xls", 1); Although the sibling folder is not Index1.xls, but the system will be c:/handsome man's BT file/Go down to find Index1.xls. Context: This is a god-like parameter. This is what many textbooks say: "A set of options that can modify the behavior of a stream." This sentence is more difficult to understand than the concept of "capitalism" in textbooks and the concept of another "doctrine" of the same kind. This is actually the argument. File_get_contents can not only read local files, but also read Web files. If we want to read www.shenyisyn.org (note: Author Humble studio website) Home Source code, then we will need to set some parameters, such as HTTP parameters: $options = Array (' http ' =>array (' method ' = ') ' Get ',//represents using Get mode to access ' header ' = ' content-type:text/html;charset=gb2312 '. php_eol.//representative code is Chinese gb2312 ' cookie:xxxxx '. Php_eol//With cookies, if you are going to attack my site is likely to use OH)); $context = Stream_context_create($options); Then you can use File_get_contents ("www.shenyisyn.org", 0, $context "); To get it directly. Since this function is not recommended for real web content, and there is no real crawler or index software to use PHP to do, so there is not much extension to explain the use of this function in obtaining a remote address. PHP should let it do its most suitable things, especially I see some Daniel with the PHP desktop runtime to do desktop software, Jesus Christ, the spirit of research is commendable, but it is never recommended that you spend too much effort to study. Have extra time to take the children to cheat the wife, or everyone together to discuss how to "further deepen and carry forward" the construction of spiritual civilization is more practical and noble. Start,max_length: Starts indexing and gets the maximum number of bytes. Well understood, but it's not much use. 3, Php_eol This knowledge point is still a little important. \ n Soft return: Represents a newline in windows and returns to the beginning of the next line. In Linux, Unix only represents line breaks, but does not go back to the beginning of the next line. \ r Soft space: in Linux, Unix represents the return to the beginning of the line. Represents a newline in Mac OS and returns to the beginning of the next line, equivalent to the effect of \ n in Windows. \ t jumps (moves to the next column) they are valid in a double-quote or delimiter-represented string and are not valid in a single-quote string. \ r \ n generally used together, used to indicate the key on the keyboard return (Linux,unix), can also be used only \ n (windwos), in Mac OS with \ r to indicate enter! \ t represents the "tab" key on the keyboard. NewLine symbol in file: Windows: \ n Linux,unix: \ r \ n Nonsense for a long time, summed up is to ensure that the future porting to different platforms convenient. You should use Php_eol instead of \ r or \ \ \ \ \ \ \ \ \ \ \ \ \ \ \. PHP will help you resolve automatically on different platforms. Programmers must learn to give the tedious things to the system to do. Don't remember the things that are likely to confuse you. 4, PHP file processing of other functions, here is a simple list, because the general real projects rarely do too much work on the file, so some functions with the sincerity is not many. So here's just a list: file (path,include_path,context)---Read the files and read the array, splitting them according to the newline character. Last day, this function is really good, for TXT file, directly can read the array, save we also have to be separated according to the line break. fopen (Filename,mode,includE_path,context). Open the file and return the handle. Someone will ask what is called a handle, $file =fopen ("filename"); This $file is a handle. For example, my name is judging, I am the file, and my name is the handle. No special meaning, don't too tangled, otherwise too tangled at night easy urinate times too much. Fwrite (file,string,length). Write a file function. We can see the difference between file_put_contents and this function by degrees Niang. ReadFile (Filename,include_path,context) The function reads in a file and writes to the output buffer. This function is characterized by the output of the number of bytes that are read from the file. I seldom use it, because it used to be a file_get_contents this function, the result in the page has always appeared a strange figure, after my day and night reflection only found clues, annoyed incomparable. FileSize returns the size (in bytes) of the file. This function is useful, but it will have a cache. Why is it? For example, according to the requirements of the handsome man, measured the size of the Index1.xls file is 12306KB, careless I eat a meal of time, the handsome man hand Index1.xls added a bit of data or deleted, I if eat after dinner to execute filesize (' Index1.xls ') Will find the 12306KB. This is the cache, the clear cache needs to use Clearstatcache (), functions such as $size =filesize (' Index1.xls '); Echo $size; ..... Here the handsome man quietly to modify, after modification, very irresponsible to go home to wash and sleep .... Clearstatcache ();//I must clear the cache. echo filesize (' Index1.xls '); This is true, and the feof (file) function detects whether the end of the file has been reached. Not commonly used, the official textbook says that the traversal of unknown length files is useful and misleading. PHP should definitely not be used to manipulate some "unknown length" files, this is not what PHP should do. Unlink (filename,context) deletes the file. Don't say, I do not understand you will understand. Copy (source,destination) copies the file. Remember one thing: There is no file, there is, anyway, a word "resolute will cover." mkdir (Path,mode,recursive,context) to create a directory. This is a good understanding, the main Note mode: Default all Permissions. There is also the recursive: whether recursive. For example, you want to create 1/2/3/4/5 such a level of folder, in fact, the most important thing is that you want to create "5" This folder, if this parameter is False, then if the previous 1/2/3/4 is not so will create a failure, if set to true, the system will automatically help you to create the 1/2/3/4/together. RmDir (Path). Delete the folder. It is still safe to say this function. If the folder is not empty, it will not bird you. First write so much, here contains most of the PHP file operation, of course, there are many, because the space is not all enumerated, welcome to add a story, have characteristics, need to use the file operation function carefully. If there is no, improper, insufficient, nonsense part of the above please understand and correct.
http://www.bkjia.com/PHPjc/477564.html www.bkjia.com true http://www.bkjia.com/PHPjc/477564.html techarticle the motto: Reading and writing files is one of the only things our programmers do on an empty night when there is no database in the year (perhaps not necessarily oh ~ ~ ~). So even now ...