PHP File Handling Function

download PHP File Handling Function

of 33

Transcript of PHP File Handling Function

  • 8/10/2019 PHP File Handling Function

    1/33

    File handling Function

    1

  • 8/10/2019 PHP File Handling Function

    2/33

    fopen()

    The fopen() function is used to open files in PHP.

    The first parameter of this function contains the

    name of the file to be opened and the secondparameter specifies in which mode the file should

    be opened.

    If fopen is unable to open the file, it returns 0

    (zero) otherwise it will return 1.

    2

  • 8/10/2019 PHP File Handling Function

    3/33

    Syntax and Example

    var_name=fopen(file_name,file_open_mode)

  • 8/10/2019 PHP File Handling Function

    4/33

    The file may be opened in one of the following modes

    Modes Description

    r Read only. Starts at the beginning of the file

    r+ Read/Write. Starts at the beginning of the file

    w Write only. Opens and clears the contents of

    file; or creates a new file if it doesn't exist

    w+ Read/Write. Opens and clears the contents of

    file; or creates a new file if it doesn't exist

    a Append. Opens and writes to the end of the

    file or creates a new file if it doesn't exist

    a+ Read/Append. Preserves file content by

    writing to the end of the file

    x Write only. Creates a new file. Returns

    FALSE and an error if file already exists

    x+ Read/Write. Creates a new file. Returns

    FALSE and an error if file already exists

    4

  • 8/10/2019 PHP File Handling Function

    5/33

    fwrite()

    The fwrite function allows data to be written to

    any type of file and an open file.

    The function will stop at the end of the file orwhen it reaches the specified length, whichever

    comes first.

    This function returns the number of bytes written

    on success, or FALSE on failure.

    5

  • 8/10/2019 PHP File Handling Function

    6/33

    Syntax and Example

    var_name =fwrite(file,string,length)

  • 8/10/2019 PHP File Handling Function

    7/33

    fread()

    The fread() reads from an open file.

    The function will stop at the end of the file or

    when it reaches the specified length, whichevercomes first.

    fread() function returns the read string on success,or FALSE on failure.

    7

  • 8/10/2019 PHP File Handling Function

    8/33

    Syntax and Example

    Syntax:

  • 8/10/2019 PHP File Handling Function

    9/33

    $myFile = "testFile.txt";

    $fh = fopen($myFile, 'r');

    $theData = fread($fh, 5);

    fclose($fh);

    echo $theData;

    9

  • 8/10/2019 PHP File Handling Function

    10/33

    $myFile = "testFile.txt";

    $fh = fopen($myFile, 'r');

    $theData = fread($fh, filesize($myFile));

    fclose($fh); echo $theData;

    10

  • 8/10/2019 PHP File Handling Function

    11/33

    fclose()

    The fclose() function closes an open file.

    fcose() function returns TRUE on success or

    FALSE on failure.

    11

  • 8/10/2019 PHP File Handling Function

    12/33

    Syntax and Example

    Syntax:

    fclose(file)

  • 8/10/2019 PHP File Handling Function

    13/33

    file_exists()

    file_exists() function is used to check whether a

    file or directory exists or not.

    Return TRUE if the file or directory specifiedby file_name exists otherwise FALSE.

    13

  • 8/10/2019 PHP File Handling Function

    14/33

    Syntax and Example

    Syntax:

    14

    var_name=file_exists (file_name)

  • 8/10/2019 PHP File Handling Function

    15/33

    is_readable

    The is_readable() function is used to check whether

    the specified file is readable or not.

    TRUE if file_name exists and is readable, FALSE

    otherwise.

    15

  • 8/10/2019 PHP File Handling Function

    16/33

    Syntax and Example

    is_readable(file_name)

    16

  • 8/10/2019 PHP File Handling Function

    17/33

    is_writable()

    The is_writable() function is used to check whether

    the specified file is writeable.

    TRUE if file_name exists and is writable.

    17

  • 8/10/2019 PHP File Handling Function

    18/33

    Syntax and Example

    is_writable(file_name)

    18

  • 8/10/2019 PHP File Handling Function

    19/33

    fgets()

    The fgets() function is used to get line from file

    pointer of an open file.

    The fgets() function stops returning on a new line, atthe specified length, or at EOF, whichever comes

    first.

    This function returns FALSE on failure.

    19

  • 8/10/2019 PHP File Handling Function

    20/33

    Syntax and Example

    fgets(file_handler, byte_length)

    20

    Name Descriptionfile_handler When a file is successfully opened by fopen() or

    fsockopen() it returns a resource ID, which isreferred as file handler or file pointer. The fgets()function uses this ID to return a series of bytes

    from an open filebyte_length Specifies the number of bytes to read. Default

    length is 1024.

  • 8/10/2019 PHP File Handling Function

    21/33

    21

  • 8/10/2019 PHP File Handling Function

    22/33

    fgetc()

    The fgetc() function reads a single character from a

    open file pointed by file handler..

    A string containing a single character and FALSE onEOF.

    22

  • 8/10/2019 PHP File Handling Function

    23/33

    Syntax and Example

    fgetc(file_handler)

    23

  • 8/10/2019 PHP File Handling Function

    24/33

    file()

    The file() reads whole file into an array.

    Each array element contains a line from the file, with

    newline still attached.

    24

  • 8/10/2019 PHP File Handling Function

    25/33

    Syntax and Example

    file(path,include_path,context)

    25

  • 8/10/2019 PHP File Handling Function

    26/33

    file_get_contents()

    The file_get_contents() reads a whole file into astring.

    file_get_contents (file_name, include_path_name, context,

    star_position, max_length)

    26

  • 8/10/2019 PHP File Handling Function

    27/33

    Parameter Description

    path Required. Specifies the file to read

    include_path Optional. Set this parameter to '1' if you want to search for

    the file in the include_path (in php.ini) as well

    context Optional. Specifies the context of the file handle. Context is a

    set of options that can modify the behavior of a stream. Canbe skipped by using NULL.

    start Optional. Specifies where in the file to start reading. Thisparameter was added in PHP 5.1

    max_length Optional. Specifies how many bytes to read. This parameter

    was added in PHP 5.1

    27

  • 8/10/2019 PHP File Handling Function

    28/33

    Example

    28

  • 8/10/2019 PHP File Handling Function

    29/33

    file_put_contents()

    The file_put_contents() function writes a stringto a file.

    29

    file_put_contents(file,data,mode,context)

  • 8/10/2019 PHP File Handling Function

    30/33

    Syntax and Example

    Parameter Description

    file Required. Specifies the file to write to. If the file does notexist, this function will create one

    data Required. The data to write to the file. Can be a string, an array or adata stream

    mode Optional. Specifies how to open/write to the file. Possible values:

    FILE_USE_INCLUDE_PATHFILE_APPENDLOCK_EX

    context Optional. Specifies the context of the file handle. Context is a set ofoptions that can modify the behavior of a stream.

    30

  • 8/10/2019 PHP File Handling Function

    31/33

    31

  • 8/10/2019 PHP File Handling Function

    32/33

    ftell()

    The ftell() function is used to fetch the current

    position of the file pointer of an open file.

    32

    ftell(file_handler)

  • 8/10/2019 PHP File Handling Function

    33/33

    Questions?