PHP File IO Opening a file - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

PHP File IO Opening a file

Description:

PHP File IO Opening a file – PowerPoint PPT presentation

Number of Views:167
Avg rating:3.0/5.0
Slides: 8
Provided by: capi7
Category:
Tags: php | appending | file | opening

less

Transcript and Presenter's Notes

Title: PHP File IO Opening a file


1
PHP File IO Opening a file
  • Use the resource fopen( )
  • Resource fopen ( string filename, string mode)
  • filename name of file
  • mode read, write, append

2
PHP File IO Opening a file
  • fopen( ) associates a resource, specified by
    filename, to a stream
  • If filename is of the form "scheme//...", it is
    assumed to be a URL and PHP will search for a
    protocol handler for that scheme
  • If PHP has decided that filename specifies a
    local file, then it will try to open a stream on
    that file
  • The file must be accessible to PHP ? file needs
    appropriate access permissions

3
PHP File IO Opening a file mode
  • r ? read only
  • r ? read and write
  • w ? write only
  • w ? write and read
  • a ? append only
  • a ? append and read

4
PHP File IO - Reading
  • Open a file
  • fp fopen("test.txt","r")
  • // read 10 bytes
  • contents fread(fp, 10)
  • fclose(fp)

5
PHP File IO - Reading
  • Open a file
  • fp fopen("test.txt","r")
  • // Read the whole file
  • contents fread(fp, filesize("test.txt"))
  • fclose(fp)

6
PHP File IO - Writing
  • handle fopen("test2.txt", "w")
  • written fwrite(handle, "hi php")
  • echo "written bytes writtenltbrgt"
  • fclose(handle)

7
PHP File IO - Appending
  • handle fopen("test2.txt", "a")
  • written fwrite(handle, "appending\n")
  • echo "written bytes writtenltbrgt"
  • fclose(handle)
Write a Comment
User Comments (0)
About PowerShow.com