File handling with php
Download
1 / 32

File Handling with PHP - PowerPoint PPT Presentation


  • 125 Views
  • Uploaded on

File Handling with PHP . Objectives. Open and close files Write data to files Read data from files Manage files and directories. Files and PHP. File Handling Data Storage Though slower than a database Manipulating uploaded files From forms Creating Files for download.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' File Handling with PHP ' - sauda


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Objectives
Objectives

  • Open and close files

  • Write data to files

  • Read data from files

  • Manage files and directories


Files and php
Files and PHP

  • File Handling

    • Data Storage

      • Though slower than a database

    • Manipulating uploaded files

      • From forms

    • Creating Files for download


Opening and closing file streams
Opening and Closing File Streams

  • A stream is a channel used for accessing a resource that you can read from and write to

  • The input stream reads data from a resource (such as a file)

  • The output stream writes data to a resource

    1. Open the file stream with the fopen() function

    2. Write data to or read data from the file stream

    3. Close the file stream with the fclose() function


Opening a file stream
Opening a File Stream

  • A handle is a special type of variable that PHP uses to represent a resource such as a file

  • The fopen() function opens a handle to a file stream

  • The syntax for the fopen() function is:

    open_file = fopen(“text file”, “mode”);

  • A file pointer is a special type of variable that refers to the currently selected line or character in a file


Opening a file stream continued
Opening a File Stream (continued)

Mode arguments of the fopen() function



Opening a file stream continued1
Opening a File Stream (continued)

$BowlersFile = fopen(“bowlers.txt”, “r+”);

Figure 1 Location of the file pointer when the fopen() function uses a modeargument of “r+”


Opening a file stream continued2
Opening a File Stream (continued)

$BowlersFile = fopen(“bowlers.txt”, “a+”);

Figure 2 Location of the file pointer when the fopen() function uses a modeargument of “a+”


Php file io reading
PHP – File IO - Reading

  • Open a file

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

  • // Read 10 bytes

  • $contents = fread($fp, 10);

  • fclose($fp);


Php file io reading1
PHP – File IO - Reading

  • Open a file

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

  • // Read the whole file

  • $contents = fread($fp, filesize("test.txt"));

  • fclose($fp);


Php file io writing
PHP – File IO - Writing

  • $handle = fopen("test.txt", "w");

  • $written = fwrite($handle, "hi php");

  • echo "$written bytes written<br>";

  • fclose($handle);


Php file io appending
PHP – File IO - Appending

  • $handle = fopen("test.txt", "a");

  • $written = fwrite($handle, "appending\n");

  • echo "$written bytes written<br>";

  • fclose($handle);


File open close example
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);

?>


Now what
Now what..?

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

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


Reading data
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.


Reading data1
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.


Data reading example
Data Reading Example

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

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

fclose($handle);


Data reading example1
Data Reading Example

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

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

fclose($handle);

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

Open the file and assign the resource to $handle


Data reading example2
Data Reading Example

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

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

fclose($handle);

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

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

get and echo the data line by line


Data reading example3
Data Reading Example

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

while (!feof($handle)) {

echo fgets($handle, 1024);

echo '<br />';

}

fclose($handle);

Close the file

fclose($handle);


File open shortcuts
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.


Writing data
Writing Data

  • To write data to a file use:

  • fwrite($handle,$data)

    • Write $data to the file.


Data writing example
Data Writing Example

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

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

fclose($handle);


Data writing example1
Data Writing Example

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

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

fclose($handle);

Open file to append data (mode 'a')

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

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

Write new data (with line break after previous data)


Other file operations
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


Dealing with directories
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'


Directory example
Directory Example

$handle = opendir('./');

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

{

echo"$file<br />";

}

closedir($handle);


Directory example1
Directory Example

$handle = opendir('./');

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

{

echo"$file<br />";

}

closedir($handle);

$handle = opendir('./');

Open current directory


Directory example2
Directory Example

$handle = opendir('./');

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

{

echo"$file<br />";

}

closedir($handle);

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

{

echo"$file<br />";

}

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


Directory example3
Directory Example

$handle = opendir('./');

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

{

echo"$file<br />";

}

closedir($handle);

closedir($handle);

Close the directory stream


Other directory operations
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


ad