WRITE TO A FILE

  5 WORK WITH FILES PHP WRITE TO A FILE

  There are several different access modes you can use when opening a file. Each access mode places the file position indicator in a specific location. t is often necessary to access text files stored on You can use the fputs function to write a line of the Web server from within a PHP page. Writing information to the file. The fputs function usually takes

ACCESS MODE: DESCRIPTION:

  data to text files allows you to store data, such as two arguments––a file pointer and the information to be

  I

  r configuration settings and application information. written to the file. You may also specify the maximum Open a file for reading only. Place the file position indicator length of the line to be written, in bytes. After the at the start of the file.

  Before you can write to a file, you must establish a maximum length has been reached, any extra information r+ connection to the file by using the fopen function to

  Open a file for reading and writing. Place the file position at the end of a line will not be written to the file. open a file and create a file pointer. A file pointer indicates indicator at the start of the file. the file you want to work with. The fopen function takes Once all the information has been written to a file, the w

  Open a file for writing only. Place the file position indicator two arguments––the name of the file to be opened and fclose function should be used to close the connection at the start of the file. Create the file if it does not exist. the access mode you want to use. to the file. w+

  Open a file for reading and writing. Place the file position To open a file for writing, the w access mode can be As with other operations involving accessing a file, the indicator at the start of the file. Create the file if it does specified. This access mode creates a new file if the file proper permissions that allow the file to be written to not exist. does not already exist. Using the w access mode will also must be in place. For information about permissions, you overwrite any information already in an existing file. should consult your operating system's documentation. a

  Open a file for writing only. Place the file position indicator at the end of the file. Create the file if it does not exist. When a file is opened for writing, the file position indicator is placed at the start of a file. The file position a+

  Open a file for reading and writing. Place the file position indicator indicates where the next operation will be indicator at the end of the file. Create the file if it does performed in the file. not exist.

  WRITE TO A FILE data - Notepad ⁄ ¤ › Á ‡ °

  ‚

  To open a file and Between the parentheses, To write a line of Type a line of Repeat steps 4 to 6 To close the file, When the PHP page You can use your text

  ■

  create a file pointer, type the name of the file you information to the file, information you want until you have specified type fclose() . is displayed in a Web editor to open the file and

  fputs()

  type a variable name for want to open followed by a type . to write to the file. all the information you browser, the information view its contents or use a

  ·

  Between the the file pointer followed comma. want to write to the file. will be written to the file. PHP page to read the file.

  ˇ

  Between the parentheses, ■ You may also specify parentheses, type

  = fopen() by .

  For information about

  ‹

  To specify the access type the name of the file the maximum length of the name of the file reading a file using a PHP mode that allows you to pointer followed by a comma. the line of information, pointer. page, see page 126.

  "w" write to the file, type . in bytes.

  5 WORK WITH FILES PHP READ A FILE

  Most computers that use Web server software The file function may be used to fter creating a file pointer and opening a file for The fgets function will read a line of text up to the and PHP have sophisticated username and automatically open, read and close a file. reading, you can access the contents of the file. maximum length specified or until it reaches a newline password-based security features. When using The file function takes the name of a

  This allows you to display the data in a Web browser character. A line will be truncated if it is longer than the PHP to read a file, the correct permissions that file as an argument and returns the entire

  A

  or compare the data in the file to other data. When opening maximum length specified. When the fgets function allow the PHP script to read the file must be contents of the file as an array. Each the file, you must specify an access mode that allows reading is used again, it will read the next line in the file and not enabled on the Web server that stores the file. element in the array will correspond to from the file. The r access mode is typically used. the remainder of the previous line that was truncated.

  Web servers are typically configured to use a line of information in the file. You do When a file is initially opened for reading, the file position The feof function can be used to determine if the end a special user account, such as web or http, not need to create a file pointer when indicator is placed at the beginning of the file. The fgets of a file has been reached. The feof function takes the to allow access to files. This user account using the file function. function is used to read the line of text indicated by the file file pointer as an argument and is usually used with the must have read permissions for the files to

  Example:

  position indicator and must have two arguments––the file Not operator (!) as the condition tested in a while loop. be opened because when PHP attempts to

  $lines = file("data.txt") ;

  pointer and the maximum length of the line to be read, read a file, the operating system interprets it

  foreach ($lines as $value)

  Once all the lines from a file have been read, the fclose in bytes. After a line of text is read from the file, the file as an attempt to read the file by the username

  { function should be used to close the connection to the file.

  position indicator automatically moves to the next line assigned to the Web server. The special user

  print $value . "<br>"; of text in the file.

  account on the Web server must be set up } before files can be accessed using PHP. A while loop is often used to process each line in a file. With each iteration of the loop, the information retrieved from the file using the fgets function can be assigned to a variable and displayed to the client using the print function.

  READ A FILE ⁄ ¤ ‹ ˇ Á ‡ ·

  Perform steps 1 and 2 To specify the access To read a line of Type the maximum Create a while loop To close the file, type Display the PHP page ■ The Web browser on page 124 to enter the mode that allows you to information from the length of the line of that will read and display fclose() . in a Web browser. displays the result of

  fgets()

  code that opens a file and read information from file, type . information to be read, each line of information reading a file.

  °

  Between the

  r creates a file pointer. the file, type " ".

  in bytes. from the file.

  ›

  Between the parentheses, type the parentheses, type the name of the file pointer. name of the file pointer followed by a comma.

  5 WORK WITH FILES PHP

  In order to successfully copy and delete files, you You can use the filesize function

  COPY OR DELETE A FILE must have the appropriate file and operating system to determine the size of a file in bytes.

  permissions on the computer where the files are Determining the size of a file is useful HP provides functions that allow you to manage files

  The unlink function derives its name from the command stored. For example, you cannot delete a file that when you want to delete large files in from within a PHP page. For example, the copy used to delete files on Unix computers. The unlink function has read-only permissions set. For information order to free up storage space. function creates a copy of a file, while the unlink takes the filename or path of the file you want to delete as its about the permissions for an operating system,

  P

  function deletes a file. Copying a file is useful for creating argument. refer to the operating system's documentation.

TYPE THIS:

  a backup copy of a file. Deleting a file lets you remove a file When specifying arguments for the copy and unlink

  print "The size of the file is: "; that is no longer needed by the PHP page.

  The copy function is also useful for moving a file print filesize("formdata.txt") . " bytes"; functions, if the file you want to copy or delete is located in It is good programming practice to verify whether a file exists from one directory to another. To move a file, you the current directory, you can specify just the name of the before attempting to copy or delete the file. You can use the use the copy function to create a copy of the file file. If the file is located in a different directory, you must file_exists function to determine whether a file exists. in the directory you want to move the file to. You specify the full path to the file. You may want to store the

  RESULT:

  The file_exists function returns a value of true if a file can then use the unlink function to remove the name or path of a file in a variable and then use the variable

  The size of the file is: 1867 bytes you specify exists and a value of false if the file does not original file from the directory you no longer want with the copy or unlink function. exist. to store the file.

  When the copy and unlink functions are successful, they

  Example:

  The copy function takes two arguments––the filename or return an integer value other than 0. If the functions are not path of the file to be copied and the filename or path of the

  copy("formdata.txt", "backup/webfile/formdata.txt"); successful, they return a value of 0. unlink("formdata.txt");

  copy you want to create. When the file is copied, the copy function duplicates the file without removing the original file.

  COPY A FILE DELETE A FILE

  ⁄ copy() ‹ › ⁄ ¤ ‹

  To copy the file, type . Type a comma and Display the PHP page ■ The file has been To delete a file, type Between the parentheses, Display the PHP page ■ The file has been

  unlink() then type the name or in a Web browser. copied. . type the name or path of in a Web browser. deleted.

  ¤

  Between the parentheses, path of the copy you the file you want to remove type the name or path of the want to create enclosed enclosed in quotation marks. file you want to copy enclosed in quotation marks. in quotation marks.

  5 WORK WITH FILES PHP DETERMINE THE STATUS OF A FILE

  Determining the status of a file can improve You can use the is_dir function to determine the efficiency of a PHP script. For example, whether an item is a directory. The is_dir hen working with files, it is often necessary To determine whether a file can be written to by a PHP before performing a complex process, such function returns a value of true if the directory to determine information about a file before page, you use the is_writable function. If the file as retrieving information from a database you specify exists and is a directory. performing an action. For example, you should exists and can be written to, the function returns a value and appending the information to a file, a

  W

  verify that a file can be written to by a PHP page before of true. The is_writable function is commonly used to check can be made to ensure the data can

TYPE THIS:

  opening the file for writing. check the status of files that can be written to by multiple be written to the file.

  if ( is_dir("c:\data\webinfo" ))

  users, since such files are frequently temporarily

  {

  The is_file function allows you to determine if a file unavailable for writing while in use by another user.

  The is_file, is_readable and print "c:\data\webinfo is a directory."; you specify is a file. This is useful when you want to

  }

  is_writable functions may be affected by ensure that an item is a file rather than a directory or a The is_file, is_readable and is_writable

  else

  the operating system you are running and the link to another file. The is_file function is commonly functions all take a single argument that indicates the

  {

  file system used by the computer that stores used to verify that an item is a file before attempting to file you want to check. The argument can be the file

  print "This is not a directory.";

  the files. While the is_file, is_readable open the file. The function returns a value of true if the name or path of the file, such as c:\webfiles\form.dat,

  }

  and is_writable functions should perform item you specify is a file. or a variable that stores the filename or path, such as as expected on Unix systems, there may

  $filename. If the file you want to check is in the current Before opening a file you want to read, it is common be incompatibilities that interfere with directory, you can specify just the name of the file. If the

  RESULT:

  practice to use the is_readable function to verify that the operation of these functions on other file is located in a different directory, you must specify the file can be read from a PHP page. If a file exists and systems. c:\data\webinfo is a directory. the full path of the file. is readable, the is_readable function returns a value of true.

  DETERMINE THE STATUS OF A FILE ⁄ ‹ ˇ ‡ ° ‚ —

  To store the path of a Between the parentheses, To determine whether the Type the code that To determine whether Type the code that uses Display the PHP page The Web browser

  ■

  file you want to check in type the name of the variable file can be read from the PHP uses the result of you can write to the file, the result of determining in a Web browser. displays the results of

  is_readable() is_writable()

  a variable, type the code that stores the path of the file. page, type . determining whether type . whether the file is writable. determining the status that assigns the path to the file is readable. of a file.

  › Á ·

  Type the code that uses the Between the parentheses, Between the parentheses, the variable. result of determining whether type the name of the variable type the name of the variable

  ¤ To determine whether the file is a file. that stores the path of the file.

  that stores the path of the file. the file is a file, type

  is_file() .

  5 WORK WITH FILES PHP

  Only the owner of a file or the system The chown function can be used to change administrator can change the group or the owner of a file. When using the chown permissions for a file. When using the function, you must specify the location and

SET FILE GROUP AND PERMISSIONS ON UNIX

  chgrp function, the owner of a file can name of the file and the user name of the assign the file only to a group to which the new owner. Only the system administrator ou can use a PHP page to change the group a file dash followed by a series of nine characters. The first three owner belongs. The system administrator can use the chown function. belongs to and set the permissions for the file. characters represent the permissions for the file's owner. can assign a file to any group.

  Example:

  Changing the group and permissions for a file The next three characters represent the permissions for the

  Y chown("/var/www/html/file1", "maryc")

  allows you to restrict access to the file. owner's group and the last three characters represent the permissions for all other users. The system function allows you to execute UNIX commands from within a PHP page. To view the group To change the permissions for a file, you use the chmod

  The fileowner function allows you to determine the owner of a and permissions for each file in the current directory, function. The chmod function takes two arguments––the file. You can use the filegroup function to determine the group you can use the ls -l command. location and name of the file and the octal value representing a file belongs to, which is usually the group to which the owner the file permissions you want to use. The octal value consists belongs. These functions retrieve an ID number that represents The chgrp function is used to assign a file to a new group. of three digits, which represent the permissions for the the user name of the owner or the name of the group. You can

  To use the chgrp function, you specify the location and owner, followed by the permissions for the owner's group use the password file or group file available in Unix to match an name of the file you want to assign to a new group followed and the permissions for other users. For example, the ID number with the corresponding user name or group name. by the name of the group.

  • rwxr-xr-x permissions will have an octal value of 755. You should precede the octal value with a zero (0).

  Unix systems allow you to assign any combination of

TYPE THIS:

  RESULT:

  read (r), write (w), and execute (x) permissions to a file's The chgrp and chmod functions return a value of true

  print ("The UID is: " . fileowner("/var/www/html/file1")) . "br";

  The UID is: 48 owner, the group the file belongs to and all other users. when the functions are executed successfully.

  print ("The GID is: " . filegroup("/var/www/html/file1"));

  The GID is: 501 The permissions for a file are typically represented by a

SET FILE GROUP AND PERMISSIONS ON UNIX

  ⁄ ¤ ˇ

  ° ‚

  To execute a Unix To display a list of files CHANGE FILE GROUP Type a comma, SET FILE PERMISSIONS Type a comma, followed by Display the PHP page The Web browser

  ■

  command from the PHP in the current directory followed by the name the octal value that represents in a Web browser. displays the results of

  ‹ Á

  To assign a file to a new To change the page, type system() . and the permissions for of the group to which the permissions you want to changing the group and group, type chgrp() . permissions for a file, each file, type "ls –l" you want to assign assign to the file. The octal permissions for a file. type chmod() .

  ›

  between the parentheses. Between the parentheses, the file, enclosed in value should be preceded by 0.

  ‡

  type the location and name quotation marks. Between the

  ·

  To display an updated of the file, enclosed in parentheses, type the list of the files in the current quotation marks. location and name of directory, repeat steps 1 and 2. the file, enclosed in

  5 WORK WITH FILES PHP

  PHP allows you to specify permissions for a directory in In order to successfully create and remove octal notation. When creating a directory on a computer directories, you must have the appropriate You can make a variable name easier to read running the UNIX operating system, you can convert file and operating system permissions on

CREATE AND DELETE DIRECTORIES

  by using the underscore character ( _ ) to UNIX permissions to octal notation using the following the computer where the directories are to separate the words in the name. When you chart. The UNIX permissions include read (r), write (w) be created or removed. For information separate the words in a variable name, you

  HP provides functions that allow you to manage A directory you want to remove must not contain any files and execute (x). The octal value will consist of three about the permissions for an operating should capitalize each word. directories from within a PHP page. The mkdir or subdirectories. You must delete any existing files and digits, representing the permissions for the owner, system, refer to the operating system's function creates a new directory, while the rmdir subdirectories from a directory before you can delete the followed by the permissions for the owner's group Example: documentation.

  P function removes an existing directory. You may want to directory. For information about deleting files, see page 128.

  and the permissions for other users. For example, Dim My_Date_Of_Birth create a directory to store temporary files and then remove a directory with permissions -rwxrw-r-- will have the

  When the mkdir and rmdir functions successfully create The chdir function allows you to change the directory when the files are no longer needed by the octal value 764. When entering the permissions in a or remove a directory, they return an integer value other which directory you are working in. This PHP page.

  PHP script, precede the octal value with a zero (0). than 0. If the functions are not successful, they return a is useful when you want to work with files When working with directories in the current directory, value of 0. stored in another directory. To change the

UNIX PERMISSIONS: OCTAL VALUE:

  you specify the name of the directory you want to create current directory, specify the name of the

  It is good programming practice to verify whether a

  • or remove. If you want to work with directories in a different

  directory you want to work in, enclosed in directory exists before attempting to create or remove directory, you must specify the full path of the directory you

  • x quotation marks.

  1 the directory. You can use the file_exists function want to create or remove. You may want to store the name

  • w-

  2 Example: to determine whether a file exists. or path of a directory in a variable and then use the variable

  chdir("temp");

  • wx

  3 with the mkdir or rmdir function. r--

  4 When creating a new directory on a computer running the r-x

  5 UNIX operating system, you must also specify permissions rw- for the directory. Permissions control access to the directory

  6 and determine the operations, such as reading and writing, rwx

  7 that can be performed.

  CREATE A DIRECTORY DELETE A DIRECTORY

  ⁄ To store the name or ‹ Between the ˇ Display the PHP page The directory has ⁄ To store the name or path ¤ To remove a directory, › Display the PHP page The directory has ■

  ■ path of the directory you parentheses, type the in a Web browser. been created. of the directory you want to type rmdir() . in a Web browser. been removed.

  want to create in a variable, name of the variable remove in a variable, type

  ‹ Between the parentheses, type the code that assigns the you created in step 1.

  the code that assigns the type the name of the variable information to the variable. information to the variable.

  › If necessary, type a you created in step 1. ¤ To create a directory, comma and then type

  type mkdir() . the octal value for the permissions you want