25
Chapter - 5 Handling Files PHP: Hypertext Preprocesso

Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Embed Size (px)

Citation preview

Page 1: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Chapter - 5

Handling Files

PHP: Hypertext Preprocessor

Page 2: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Outline

• Overview

• Opening a file

• How to create a file ?

• Closing a File

• Check End-of-file

• Reading a File Line by Line

• Reading a File Character by Character

Page 3: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Overview

We can after this chapter to understanding of all

types of file manipulation in PHP, like create,

open, and close a file. After establishing those

basics, we will then cover other important file

tasks, such as: read, write, append, truncate, and

uploading files with PHP.

Page 4: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Overview

When you are manipulating files you must be very

careful because you can do a lot of damage if you

do something wrong. Common errors include

editing the wrong file, filling a hard-drive with

garbage data, and accidentally deleting a file's

contents.

Page 5: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Opening a File

In PHP, a file is created using a command that is also used to

open files. It may seem a little confusing, but we'll try to clarify

this conundrum.

In PHP the fopen function is used to open files. However, it can

also create a file if it does not find the file specified in the

function call. So if you use fopen on a file that does not exist, it

will create it, given that you open the file for writing or appending

(more on this later).

Page 6: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

How to create a file ?

fopen() binds a named resource, specified by filename, to a

stream.

Returns a file pointer resource on success, or FALSE on

error.

Note: If the fopen() function is unable to open the specified

file, it returns 0 (false).

resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )

<?php

$file=fopen("welcome.txt","r");

?>

Page 7: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

How to create a file ?

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

Page 8: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

How to create a file ?

<?php

$file=fopen("welcome.txt","r") or exit("Unable to open file!");

?>

Page 9: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Closing a File

The file pointed to by handle is closed.

Returns TRUE on success or FALSE on failure.

bool fclose ( resource $handle )

<?php$file = fopen("test.txt","r");

//some code to be executed

fclose($file);?>

Page 10: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP fread() Function

The fread() reads from an open file.

The function will stop at the end of the file or when it reaches the

specified length, whichever comes first.

This function returns the read string, or FALSE on failure.

fread(file,length)

Parameter Description

file Required. Specifies the open file to read from

length Required. Specifies the maximum number of bytes to read

Page 11: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP fread() Function

<?php

$file = fopen("test.txt","r");

fread($file,"10");

fclose($file);

?>

Page 12: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP fread() Function

<?php

$file = fopen("test.txt","r");

fread($file,"10");

fclose($file);

?>

Page 13: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP fread() Function

<?php

$file = fopen("test.txt","r");

fread($file,filesize("test.txt"));

fclose($file);

?>

Page 14: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Check End-of-file

The feof() function checks if the "end-of-file" (EOF)

has been reached.

The feof() function is useful for looping through

data of unknown length.

Note: You cannot read from files opened in w, a,

and x mode!

Page 15: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Check End-of-file

Tests for end-of-file on a file pointer. Returns TRUE if the file pointer is at EOF or an error occurs

(including socket timeout); otherwise returns FALSE.

bool feof ( resource $handle )

<?php

$file = “name.txt”;if (feof($file)) echo "End of

file";

?>

Page 16: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Reading a File Line by Line

Gets a line from file pointer.

Returns a string of up to length - 1 bytes

read from the file pointed to by handle.

If there is no more data to read in the file

pointer, thenFALSE is returned.

If an error occurs, FALSE is returned.

string fgets ( resource $handle [, int

$length ] )

Page 17: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Reading a File Line by Line

<?php

$file = fopen("welcome.txt", "r") or exit("Unable

to open file!");

//Output a line of the file until the end is reached

while(!feof($file))

  {

  echo fgets($file). "<br>";

  }

fclose($file);

?>

Page 18: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Reading a File Character by Character

Gets a character from the given file

pointer.

Returns a string containing a single

character read from the file pointed to

by handle. Returns FALSE on EOF.

string fgetc ( resource $handle )

Page 19: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

Reading a File Character by Character

<?php

$file=fopen("welcome.txt","r") or exit("Unable to open

file!");

while (!feof($file))

  {

  echo fgetc($file);

  }

fclose($file);

?>

Page 20: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP Directory Functions

The directory functions allow you to

retrieve information about directories and

their contents.

The mkdir() function creates a directory.

This function returns TRUE on success, or

FALSE on failure.

mkdir(path,mode,recursive,context)

Page 21: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP Directory Functions

Parameter Description

path Required. Specifies the name of the directory to create

mode Optional. Specifies permissions. By default, the mode is 0777 (widest possible

access).The mode parameter consists of four numbers:

• The first number is always zero

• The second number specifies permissions for the owner

• The third number specifies permissions for the owner's user group

• The fourth number specifies permissions for everybody else

Possible values (to set multiple permissions, add up the following numbers):

• 1 = execute permissions

• 2 = write permissions

• 4 = read permissions

recursive Optional. Specifies if the recursive mode is set (added in PHP 5)

context Optional. Specifies the context of the file handle. Context is a set of options that

can modify the behavior of a stream (added in PHP 5)

Page 22: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP Directory Functions

<?php

mkdir("testing"); // make directory or folder

?>

<?php

mkdir("testing/sub"); // make sub directory or sub

folder

?>

Page 23: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP Remove Directory Functions

The rmdir() function removes an empty directory.

This function returns TRUE on success, or FALSE on failure.

rmdir(dir,context)

Page 24: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP Remove Directory Functions

Parameter Description

dir Required. Specifies the directory to be removed

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

Page 25: Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character

PHP Remove Directory Functions

<?php

$path = "images";

if(!rmdir($path))

  {

  echo ("Could not remove $path");

  }

?>