Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP-language, files Jouni Juntunen Oulu University of Applied Sciences

Similar presentations


Presentation on theme: "PHP-language, files Jouni Juntunen Oulu University of Applied Sciences"— Presentation transcript:

1 PHP-language, files Jouni Juntunen Oulu University of Applied Sciences
School of Business and Information Management

2 Why files? To store data Simple to use No database available
When storing complex data - use database

3 Basic steps using files from application
Open file (Lock file while reading) Read/write to/from file (Release locking) Close file

4 Opening file Usually it is wise that applications don’t create file (necessary files are created manually during application setup) Files must be NEVER stored under public_html

5 fopen() Opening modes r=read w=write a=append
$filehandle=fopen(”../somefile.csv”,”w”); Opening modes r=read w=write a=append Check for more modes on PHP-manual

6 fwrite() $filehandle=fopen(”../somefile.csv”,”w”); flock($filehandle,LOCK_EX)) fwrite($filehandle,”Content to be added to file.”); flock($filehandle,LOCK_UN); Fclose($filehandle);

7 Reading a file <?php $filehandle=fopen(”…/..data.csv”,”r”); while (!feof($filehandle)) //Check if we have reached end of file { $row($filehandle, 1024); //Read current row from file print ”$row<br>”; //Print row without any formatting } fclose($filehandle); ?>

8 fclose() You’ll have to ALWAYS remember to close to used file!
$filehandle=fopen(”../somefile.csv”,”r”); fclose($filehandle); You’ll have to ALWAYS remember to close to used file!

9 Using files in Applications
Data is stored to file using e.g. delimeter character For example csv-file (Comma separated value)

10 Exercise Implement simple guestbook which stores messages to csv-file


Download ppt "PHP-language, files Jouni Juntunen Oulu University of Applied Sciences"

Similar presentations


Ads by Google