pw 02 file handling

Upload: teguhtheo

Post on 03-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 PW 02 File Handling

    1/18

    Web ProgrammingAryo Pinandito

    02 PHP File Handling

  • 7/29/2019 PW 02 File Handling

    2/18

    Files and PHP

    File Handling

    Data Storage

    Though slower than a database

    Manipulating uploaded files

    From forms

    Creating Files for download

  • 7/29/2019 PW 02 File Handling

    3/18

    Open/Close a File

    A file is opened with fopen()as a stream, andPHP returns a handle to the file that can be used toreference the open file in other functions.

    Each file is opened in a particular mode.

    A file is closed with fclose() or when your scriptends.

  • 7/29/2019 PW 02 File Handling

    4/18

    File Open Modes

    r Open for reading only. Start at beginningof file.

    r+ Open for reading and writing. Start atbeginning of file.

    w Open for writing only. Remove all previouscontent, if file doesnt exist, create it.

    a Open writing, but start at END of currentcontent.

    a+ Open for reading and writing, start at ENDand create file if necessary.

  • 7/29/2019 PW 02 File Handling

    5/18

    File Open/Close Example

  • 7/29/2019 PW 02 File Handling

    6/18

    Now what..?

    If you open a file to read, you can use more in-builtPHP functions to read data..

    If you open the file to write, you can use more in-built PHP functions to write..

  • 7/29/2019 PW 02 File Handling

    7/18

    Reading Data

    There are two main functions to read data:

    fgets($handle,$bytes)

    Reads up to $bytesof data, stops at newline or end of

    file (EOF)

    fread($handle,$bytes)

    Reads up to $bytes of data, stops at EOF.

  • 7/29/2019 PW 02 File Handling

    8/18

    Reading Data

    We need to be aware of the End Of File (EOF) point...

    feof($handle)

    Whether the file has reached the EOF point. Returns true

    if have reached EOF.

  • 7/29/2019 PW 02 File Handling

    9/18

    Data Reading Example

  • 7/29/2019 PW 02 File Handling

    10/18

    File Open shortcuts..

    There are two shortcut functions that dont requirea file to be opened:

    $lines = file($filename)

    Reads entire file into an array with each line a separateentry in the array.

    $str = file_get_contents($filename)

    Reads entire file into a single string.

  • 7/29/2019 PW 02 File Handling

    11/18

    Writing Data

    To write data to a file use:

    fwrite($handle,$data)

    Write $data to the file.

  • 7/29/2019 PW 02 File Handling

    12/18

    Data Writing Example

  • 7/29/2019 PW 02 File Handling

    13/18

    Other File Operations

    Delete fileunlink('filename');

    Rename (file or directory)

    rename('old name', 'new name');

    Copy file

    copy('source', 'destination');

    And many, many more!

    www.php.net/manual/en/ref.filesystem.php

    http://www.php.net/manual/en/ref.filesystem.phphttp://www.php.net/manual/en/ref.filesystem.php
  • 7/29/2019 PW 02 File Handling

    14/18

    Dealing With Directories

    Open a directory$handle = opendir('dirname'); $handle 'points' to the directory

    Read contents of directory

    readdir($handle) Returns name of next file in directory

    Files are sorted as on filesystem

    Close a directory

    closedir($handle) Closes directory 'stream'

  • 7/29/2019 PW 02 File Handling

    15/18

    Directory Example

  • 7/29/2019 PW 02 File Handling

    16/18

    Other Directory Operations

    Get current directorygetcwd()

    Change Directory

    chdir('dirname');

    Create directorymkdir('dirname');

    Delete directory (MUST be empty)

    rmdir('dirname');

    And more!

    www.php.net/manual/en/ref.dir.php

    http://www.php.net/manual/en/ref.dir.phphttp://www.php.net/manual/en/ref.dir.php
  • 7/29/2019 PW 02 File Handling

    17/18

    Review

    File Handling in PHP

    Can open and close files.

    Can read a file line by line or all at one go.

    Can write to files.

    Can open and cycle through the files in a directory.

  • 7/29/2019 PW 02 File Handling

    18/18

    Questions?