33
PHP Workshop 1 File Handling with PHP

Php i basic chapter 4

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Php i basic chapter 4

PHP Workshop 1

File Handling with PHP

Page 2: Php i basic chapter 4

PHP Workshop 2

Files and PHP

• File Handling– Data Storage

• Though slower than a database

– Manipulating uploaded files• From forms

– Creating Files for download

Page 3: Php i basic chapter 4

PHP Workshop 3

Open/Close a File

• A file is opened with fopen() as a “stream”, and PHP returns a ‘handle’ to the file that can be used to reference the open file in other functions.

• Each file is opened in a particular mode.

• A file is closed with fclose() or when your script ends.

Page 4: Php i basic chapter 4

PHP Workshop 4

File Open Modes‘r’ Open for reading only. Start at beginning of

file.

‘r+’ Open for reading and writing. Start at beginning of file.

‘w’ Open for writing only. Remove all previous content, if file doesn’t exist, create it.

‘a’ Open writing, but start at END of current content.

‘a+’ Open for reading and writing, start at END and create file if necessary.

Page 5: Php i basic chapter 4

PHP Workshop 5

File Open/Close Example

<?php

// open file to read

$toread = fopen(‘some/file.ext’,’r’);

// open (possibly new) file to write

$towrite = fopen(‘some/file.ext’,’w’);

// close both files

fclose($toread);

fclose($towrite);

?>

Page 6: Php i basic chapter 4

PHP Workshop 6

Now what..?

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

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

Page 7: Php i basic chapter 4

PHP Workshop 7

Reading Data

• There are two main functions to read data:• fgets($handle,$bytes)

– Reads up to $bytes of data, stops at newline or end of file (EOF)

• fread($handle,$bytes) – Reads up to $bytes of data, stops at EOF.

Page 8: Php i basic chapter 4

PHP Workshop 8

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.

Page 9: Php i basic chapter 4

PHP Workshop 9

Data Reading Example

$handle = fopen('people.txt', 'r');

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

fclose($handle);

Page 10: Php i basic chapter 4

PHP Workshop 10

Data Reading Example

$handle = fopen('people.txt', 'r');

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

fclose($handle);

Open the file and assign the resource to $handle

$handle = fopen('people.txt', 'r');

Page 11: Php i basic chapter 4

PHP Workshop 11

Data Reading Example

$handle = fopen('people.txt', 'r');

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

fclose($handle);

While NOT at the end of the file, pointed to by $handle,

get and echo the data line by line

while (!feof($handle)) {echo fgets($handle, 1024);echo '<br />';}

Page 12: Php i basic chapter 4

PHP Workshop 12

Data Reading Example

$handle = fopen('people.txt', 'r');

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

fclose($handle);

Close the file

fclose($handle);

Page 13: Php i basic chapter 4

PHP Workshop 13

File Open shortcuts..

• There are two ‘shortcut’ functions that don’t require a file to be opened:

• $lines = file($filename) – Reads entire file into an array with each line a

separate entry in the array.

• $str = file_get_contents($filename) – Reads entire file into a single string.

Page 14: Php i basic chapter 4

PHP Workshop 14

Writing Data

• To write data to a file use:• fwrite($handle,$data)

– Write $data to the file.

Page 15: Php i basic chapter 4

PHP Workshop 15

Data Writing Example

$handle = fopen('people.txt', 'a');

fwrite($handle, “\nFred:Male”);

fclose($handle);

Page 16: Php i basic chapter 4

PHP Workshop 16

Data Writing Example

$handle = fopen('people.txt', 'a');

fwrite($handle, '\nFred:Male');

fclose($handle);

$handle = fopen('people.txt', 'a');

Open file to append data (mode 'a')

fwrite($handle, “\nFred:Male”);

Write new data (with line break after previous data)

Page 17: Php i basic chapter 4

PHP Workshop 17

Other File Operations

• Delete file– unlink('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

Page 18: Php i basic chapter 4

PHP Workshop 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'

Page 19: Php i basic chapter 4

PHP Workshop 19

Directory Example

$handle = opendir('./');

while(false !== ($file=readdir($handle)))

{

echo "$file<br />";

}

closedir($handle);

Page 20: Php i basic chapter 4

PHP Workshop 20

Directory Example

$handle = opendir('./');

while(false !== ($file=readdir($handle)))

{

echo "$file<br />";

}

closedir($handle);

Open current directory$handle = opendir('./');

Page 21: Php i basic chapter 4

PHP Workshop 21

Directory Example

$handle = opendir('./');

while(false !== ($file=readdir($handle)))

{

echo "$file<br />";

}

closedir($handle);

Whilst readdir() returns a name, loop through directory contents, echoing

results

while(false !== ($file=readdir($handle)))

{

echo "$file<br />";

}

Page 22: Php i basic chapter 4

PHP Workshop 22

Directory Example

$handle = opendir('./');

while(false !== ($file=readdir($handle)))

{

echo "$file<br />";

}

closedir($handle);Close the directory stream

closedir($handle);

Page 23: Php i basic chapter 4

PHP Workshop 23

Other Directory Operations

• Get current directory– getcwd()

• Change Directory– chdir('dirname');

• Create directory– mkdir('dirname');

• Delete directory (MUST be empty)– rmdir('dirname');

• And more!– www.php.net/manual/en/ref.dir.php

Page 24: Php i basic chapter 4

PHP Workshop 24

Review

• 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.

Page 25: Php i basic chapter 4

PHP Workshop 25

Review all

• Create a File

$my_file = 'file.txt';

$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates file

Page 26: Php i basic chapter 4

PHP Workshop 26

Review all

• Open a File

$my_file = 'file.txt';

$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //open file for writing ('w','r','a')...

Page 27: Php i basic chapter 4

PHP Workshop 27

Review all

• Read a File

$my_file = 'file.txt';

$handle = fopen($my_file, 'r');

$data = fread($handle,filesize($my_file));

Page 28: Php i basic chapter 4

PHP Workshop 28

Review all

• Write to a File

$my_file = 'file.txt';

$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);

$data = 'This is the data';

fwrite($handle, $data);

Page 29: Php i basic chapter 4

PHP Workshop 29

Review all

• Append to a File

$my_file = 'file.txt';

$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);

$data = 'New data line 1';

fwrite($handle, $data);

$new_data = "\n".'New data line 2';

fwrite($handle, $new_data);

Page 30: Php i basic chapter 4

PHP Workshop 30

Review all

• Close a File

$my_file = 'file.txt';

$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);

//write some data here

fclose($handle);

Page 31: Php i basic chapter 4

PHP Workshop 31

Review all

• Delete a File

$my_file = 'file.txt';

unlink($my_file);

Page 32: Php i basic chapter 4

PHP Workshop 32

Review all

Page 33: Php i basic chapter 4

PHP Workshop 33

Review all