PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School...
-
Upload
julius-williams -
Category
Documents
-
view
212 -
download
0
Transcript of PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School...
![Page 1: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/1.jpg)
PHP-language, files
Teppo RäisänenPrincipal Lecturer
Oulu University of Applied SciencesSchool of Business and Information Management
[email protected], 050 382 6587
![Page 2: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/2.jpg)
Why files?
• To store data• Simple to use• No database available• When storing complex data - use database
![Page 3: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/3.jpg)
Basic steps using files from application
1. Open file2. (Lock file while reading)3. Read from/write to file4. (Release locking)5. Close file
![Page 4: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/4.jpg)
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
![Page 5: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/5.jpg)
fopen()
$filehandle=fopen(”../somefile.csv”,”w”);…
• Opening modesr=readw=writea=append• Check for more modes on PHP-manual
![Page 6: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/6.jpg)
fwrite()
$filehandle=fopen(”../somefile.csv”,”w”);flock($filehandle,LOCK_EX);fwrite($filehandle,”Content to be added to file.”);flock($filehandle,LOCK_UN);fclose($filehandle);
![Page 7: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/7.jpg)
Reading a file<?php$filehandle=fopen(”…/../data.csv”,”r”);while (!feof($filehandle)) //Check if we have reached end of file{
$row=fgets($filehandle); //Read current row from fileprint ”$row<br>”; //Print row without any formatting
}fclose($filehandle);?>
Note that $row=fread($filehandle, 1024) works also
![Page 8: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/8.jpg)
fclose()
$filehandle=fopen(”../somefile.csv”,”r”);fclose($filehandle);
• You’ll have to ALWAYS remember to close to used file!
![Page 9: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/9.jpg)
Using files in Applications
• Data is stored to file using e.g. delimeter character
• For example csv-file (Comma separated value)
![Page 10: PHP-language, files Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi,](https://reader035.fdocuments.in/reader035/viewer/2022072005/56649ce25503460f949ad536/html5/thumbnails/10.jpg)
Exercise
• Implement simple guestbook which stores messages to csv-file