[ Pobierz całość w formacie PDF ]
.Within the code block, we use fgets() to extract a line (or 1024 bytes) ofthe file.We assign the result to $line and then print it to the browser, appending atag for the sake of readability.Reading Arbitrary Amounts of Data from a File with fread()Rather than reading text by the line, you can choose to read a file in arbitrarilydefined chunks.The fread() function accepts a file pointer as an argument, as wellas the number of bytes you want to read.It returns the amount of data you haverequested unless the end of the file is reached first.$chunk = fread( $fp, 16 );Listing 10.10 amends our previous example so that it reads data in chunks of 16bytes rather than by the line.Listing 10.10: Reading a File with fread()1:2:3: Listing 10.10 Reading a file with fread()4:5:6:15:16:Although fread() allows you to define the amount of data acquired from a file, itwon't let you decide the position from which the acquisition begins.You can set thismanually with the fseek() function.fseek() enables you to change your currentposition within a file.It requires a file pointer and an integer representing the offsetfrom the start of the file (in bytes) to which you want to jump:fseek( $fp, 64 );Listing 10.11 uses fseek() and fread() to output the second half of a file to thebrowser.Listing 10.11: Moving Around a File with fseek()1:2:3: Listing 10.11 Moving around a file with fseek()4:5:6:18816:17:We calculate the halfway point of our file by dividing the return value of filesize() by2.We can then use this as the second argument to fseek(), jumping to the halfwaypoint.Finally, we call fread() to extract the second half of the file, printing the resultto the browser.Reading Characters from a File with fgetc()fgetc() is similar to fgets() except that it returns only a single character from a fileevery time it is called.Because a character is always 1 byte in size, fgetc() doesn'trequire a length argument.You simply need to pass it a file pointer:$char = fgetc( $fp );Listing 10.12 creates a loop that reads the file "test.txt" a character at a time,outputting each character to the browser on its own line.Listing 10.12: Moving Around a File with fseek()1:2:3: Listing 10.12 Moving around a file with fseek()4:5:6:15:18916:Writing or Appending to a FileThe processes for writing to or appending to a file are the same.The difference liesin the fopen() call.When you write to a file, you should use the mode argument "w"when you call fopen():$fp = fopen( "test.txt", "w" );All subsequent writing will occur from the start of the file.If the file doesn't alreadyexist, it will be created.If the file already exists, any prior content will be destroyedand replaced by the data you write.When you append to a file, you should use mode "a" in your fopen() call:$fp = fopen( "test.txt", "a" );Any subsequent writes to your file are added to the existing content.Writing to a File with fwrite() or fputs()fwrite() accepts a file pointer and a string.It then writes the string to the file.fputs()works in exactly the same way.fwrite( $fp, "hello world" );fputs( $fp, "hello world" );Writing to files is as straightforward as that.Listing 10.13 uses fwrite() to print to afile.We then append a further string to the same file using fputs().Listing 10.13: Writing and Appending to a File1:2:3: Listing 10.13 Writing and appending to a file4:5:6:17:18:Locking Files with flock()The techniques you have learned for reading and amending files will work fine if youare only presenting your script to a single user.In the real world, however, youwould expect many users to access your projects more or less at the same time.Imagine what would happen if two users were to execute a script that writes to onefile at the same moment.The file will quickly become corrupt.PHP4 provides the flock() function to forestall this eventuality.flock() will lock a fileto warn other process against writing to or reading from a file while the currentprocess is working with it.flock() requires a valid file pointer, and an integerrepresenting the kind of lock you would like to set.In Table 10.1 we list three kindsof locks you can apply to a file.Table 10 [ Pobierz całość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl odbijak.htw.pl
.Within the code block, we use fgets() to extract a line (or 1024 bytes) ofthe file.We assign the result to $line and then print it to the browser, appending atag for the sake of readability.Reading Arbitrary Amounts of Data from a File with fread()Rather than reading text by the line, you can choose to read a file in arbitrarilydefined chunks.The fread() function accepts a file pointer as an argument, as wellas the number of bytes you want to read.It returns the amount of data you haverequested unless the end of the file is reached first.$chunk = fread( $fp, 16 );Listing 10.10 amends our previous example so that it reads data in chunks of 16bytes rather than by the line.Listing 10.10: Reading a File with fread()1:2:3: Listing 10.10 Reading a file with fread()4:5:6:15:16:Although fread() allows you to define the amount of data acquired from a file, itwon't let you decide the position from which the acquisition begins.You can set thismanually with the fseek() function.fseek() enables you to change your currentposition within a file.It requires a file pointer and an integer representing the offsetfrom the start of the file (in bytes) to which you want to jump:fseek( $fp, 64 );Listing 10.11 uses fseek() and fread() to output the second half of a file to thebrowser.Listing 10.11: Moving Around a File with fseek()1:2:3: Listing 10.11 Moving around a file with fseek()4:5:6:18816:17:We calculate the halfway point of our file by dividing the return value of filesize() by2.We can then use this as the second argument to fseek(), jumping to the halfwaypoint.Finally, we call fread() to extract the second half of the file, printing the resultto the browser.Reading Characters from a File with fgetc()fgetc() is similar to fgets() except that it returns only a single character from a fileevery time it is called.Because a character is always 1 byte in size, fgetc() doesn'trequire a length argument.You simply need to pass it a file pointer:$char = fgetc( $fp );Listing 10.12 creates a loop that reads the file "test.txt" a character at a time,outputting each character to the browser on its own line.Listing 10.12: Moving Around a File with fseek()1:2:3: Listing 10.12 Moving around a file with fseek()4:5:6:15:18916:Writing or Appending to a FileThe processes for writing to or appending to a file are the same.The difference liesin the fopen() call.When you write to a file, you should use the mode argument "w"when you call fopen():$fp = fopen( "test.txt", "w" );All subsequent writing will occur from the start of the file.If the file doesn't alreadyexist, it will be created.If the file already exists, any prior content will be destroyedand replaced by the data you write.When you append to a file, you should use mode "a" in your fopen() call:$fp = fopen( "test.txt", "a" );Any subsequent writes to your file are added to the existing content.Writing to a File with fwrite() or fputs()fwrite() accepts a file pointer and a string.It then writes the string to the file.fputs()works in exactly the same way.fwrite( $fp, "hello world" );fputs( $fp, "hello world" );Writing to files is as straightforward as that.Listing 10.13 uses fwrite() to print to afile.We then append a further string to the same file using fputs().Listing 10.13: Writing and Appending to a File1:2:3: Listing 10.13 Writing and appending to a file4:5:6:17:18:Locking Files with flock()The techniques you have learned for reading and amending files will work fine if youare only presenting your script to a single user.In the real world, however, youwould expect many users to access your projects more or less at the same time.Imagine what would happen if two users were to execute a script that writes to onefile at the same moment.The file will quickly become corrupt.PHP4 provides the flock() function to forestall this eventuality.flock() will lock a fileto warn other process against writing to or reading from a file while the currentprocess is working with it.flock() requires a valid file pointer, and an integerrepresenting the kind of lock you would like to set.In Table 10.1 we list three kindsof locks you can apply to a file.Table 10 [ Pobierz całość w formacie PDF ]