DIWE - File handling with PHP
-
Upload
rasan-samarasinghe -
Category
Engineering
-
view
50 -
download
0
Transcript of DIWE - File handling with PHP
![Page 1: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/1.jpg)
Diploma in Web Engineering
Module VIII: File handling with PHP
Rasan SamarasingheESOFT Computer Studies (pvt) Ltd.No 68/1, Main Street, Pallegama, Embilipitiya.
![Page 2: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/2.jpg)
Contents1. include and require Statements2. include and require3. include_once Statement4. Validating Files5. file_exists() function6. is_dir() function7. is_readable() function8. is_writable() function9. is_executable() function10. filesize() function11. filemtime() function12. filectime() function13. fileatime() function14. Creating and deleting files15. touch() function16. unlink() function
17. File reading, writing and appending18. Open File - fopen()19. Close File - fclose()20. Read File - fread()21. Read Single Line - fgets()22. Check End-Of-File - feof()23. Read Single Character - fgetc()24. Seek File - fseek()25. Write File - fwrite()26. Write File - fputs()27. Lock File - flock()28. Working with Directories29. Create directory - mkdir()30. Remove directory - rmdir()31. Open directory - opendir()32. Read directory - readdir()
![Page 3: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/3.jpg)
include and require Statements
The include and require statements takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
Syntax:include 'filename';
or
require 'filename';
![Page 4: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/4.jpg)
include statement example 1 (menu.php file)
<?phpecho '<a href="/index.php">Home</a> |<a href="courses.php">Courses</a> |<a href="branches.php">Branches</a> |<a href="about.php">About Us</a> |<a href="contact.php">Contact Us</a>';?>
![Page 5: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/5.jpg)
include statement example 1 (index.php file)
<html><body>
<div><?php include 'menu.php';?></div>
<h1>Welcome to Esoft Metro Campus!</h1><p>The leader in professional ICT education.</p>
</body></html>
![Page 6: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/6.jpg)
include statement example 2 (core.php file)
<?phpfunction showFooter(){echo "<p>Copyright © " . date("Y") . " Wegaspace.com</p>";}?>
![Page 7: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/7.jpg)
include statement example 2 (index.php file)
<html><body>
<h1>Welcome to Wegaspace!</h1><p>The most unique wap community ever!</p>
<?php include 'footer.php';showFooter();?>
</body></html>
![Page 8: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/8.jpg)
include and require
The include and require statements are identical, except upon failure:
• require will produce a fatal error (E_COMPILE_ERROR) and stop the script
• include will only produce a warning (E_WARNING) and the script will continue
![Page 9: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/9.jpg)
include_once Statement
The require_once() statement will check if the file has already been included, and if so, not include (require) it again.
Syntax:include_once 'filename';
![Page 10: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/10.jpg)
include_once statement example
world.php file
<?php echo "Hello World!<br/>";?>
srilanka.php file
<?php echo "Hello Sri Lanka!<br/>";?>
![Page 11: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/11.jpg)
include_once statement example
<html><body>
<p><?php include 'world.php';include 'world.php'; // includes the file againinclude_once 'srilanka.php';include_once 'srilanka.php'; // not includes the file again?></p>
</body></html>
![Page 12: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/12.jpg)
Validating Files
• file_exists() function• is_dir() function• is_readable() function• is_writable() function• is_executable() function• filesize() function• filemtime() function• filectime() function• fileatime() function
![Page 13: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/13.jpg)
file_exists() function
The file_exists() function checks whether or not a file or directory exists.
This function returns TRUE if the file or directory exists, otherwise it returns FALSE.
Syntax:file_exists(path)
![Page 14: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/14.jpg)
file_exists() function
<?phpvar_dump(file_exists("test.txt"));?>
![Page 15: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/15.jpg)
is_dir() function
The is_dir() function checks whether the specified file is a directory.
This function returns TRUE if the directory exists.
Syntax:is_dir(file)
![Page 16: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/16.jpg)
is_dir() function
$file = "images";
if(is_dir($file)){echo ("$file is a directory");} else {echo ("$file is not a directory");}
![Page 17: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/17.jpg)
is_readable() function
The is_readable() function checks whether the specified file is readable.This function returns TRUE if the file is readable.
Syntax:is_readable(file)
![Page 18: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/18.jpg)
is_readable() function
$file = "test.txt";
if(is_readable($file)){echo ("$file is readable");} else {echo ("$file is not readable");}
![Page 19: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/19.jpg)
is_writable() function
The is_writable() function checks whether the specified file is writeable.
This function returns TRUE if the file is writeable.
Syntax:is_writable(file)
![Page 20: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/20.jpg)
is_writable() function
$file = "test.txt";
if(is_writable($file)) {echo ("$file is writeable");} else {echo ("$file is not writeable");}
![Page 21: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/21.jpg)
is_executable() function
The is_executable() function checks whether the specified file is executable.
This function returns TRUE if the file is executable.
Syntax:is_executable(file)
![Page 22: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/22.jpg)
is_executable() function
$file = "setup.exe";
if(is_executable($file)) {echo ("$file is executable");} else {echo ("$file is not executable");}
![Page 23: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/23.jpg)
filesize() function
The filesize() function returns the size of the specified file.
This function returns the file size in bytes on success or FALSE on failure.
Syntax:filesize(filename)
![Page 24: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/24.jpg)
filesize() function
echo filesize("test.txt");
![Page 25: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/25.jpg)
filemtime() function
The filemtime() function returns the last time the file content was modified.
This function returns the last change time as a Unix timestamp on success, FALSE on failure.
Syntax:filemtime(filename)
![Page 26: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/26.jpg)
filemtime() function
echo filemtime("test.txt");echo "<br />";echo "Last modified: ".date("F d Y H:i:s.",filemtime("test.txt"));
![Page 27: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/27.jpg)
filectime() function
The filectime() function returns the last time the specified file was changed.
This function returns the last change time as a Unix timestamp on success, FALSE on failure.
Syntax:filectime(filename)
![Page 28: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/28.jpg)
filectime() function
echo filectime("test.txt");echo "<br />";echo "Last change: ".date("F d Y H:i:s.",filectime("test.txt"));
![Page 29: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/29.jpg)
fileatime() function
The fileatime() function returns the last access time of the specified file.
This function returns the last access time as a Unix timestamp on success, FALSE on failure.
Syntax:fileatime(filename)
![Page 30: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/30.jpg)
fileatime() function
echo fileatime("test.txt");echo "<br />";echo "Last access: ".date("F d Y H:i:s.",fileatime("test.txt"));
![Page 31: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/31.jpg)
Creating and deleting files
• touch() function• unlink() function
![Page 32: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/32.jpg)
touch() function
The touch() function sets the access and modification time of the specified file.
This function returns TRUE on success, or FALSE on failure.
Syntax:touch(filename, time, atime)
![Page 33: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/33.jpg)
touch() function
touch("test.txt");touch("test.txt", mktime(8,40,20,2,10,1988));
![Page 34: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/34.jpg)
unlink() function
The unlink() function deletes a file.
This function returns TRUE on success, or FALSE on failure.
Syntax:unlink(filename, context)
![Page 35: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/35.jpg)
unlink() function
$file = "test.txt";
if (!unlink($file)) {echo ("Error deleting $file");} else {echo ("Deleted $file");}
![Page 36: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/36.jpg)
File reading, writing and appending
• Open File - fopen()• Close File - fclose()• Read File - fread()• Read Single Line - fgets()• Check End-Of-File - feof()• Read Single Character - fgetc()• Seek File - fseek()• Write File - fwrite()• Write File - fputs()• Lock File - flock()
![Page 37: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/37.jpg)
Open File - fopen()
The fopen() function opens a file or URL.
If fopen() fails, it returns FALSE and an error on failure.
Syntax:fopen(filename, mode, include_path, context)
![Page 38: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/38.jpg)
File open modes
Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
![Page 39: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/39.jpg)
Open File - fopen()
$file = fopen("test.txt","r");$file = fopen("/home/test/test.txt","r");$file = fopen("/home/test/test.gif","wb");$file = fopen("http://www.example.com/","r");$file = fopen("ftp://user:[email protected]/test.txt","w");
![Page 40: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/40.jpg)
Close File - fclose()
The fclose() function closes an open file.
This function returns TRUE on success or FALSE on failure.
Syntax:fclose(file)
![Page 41: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/41.jpg)
Close File - fclose()
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
![Page 42: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/42.jpg)
Read File - fread()
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.
Syntax:fread(file, length)
![Page 43: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/43.jpg)
Read File - fread()
$file = fopen("test.txt","r");
fread($file, filesize("test.txt"));
fclose($file);
![Page 44: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/44.jpg)
Read Single Line - fgets()
The fgets() function returns a line from an open file.
The fgets() function stops returning on a new line, at the specified length, or at EOF, whichever comes first.
This function returns FALSE on failure.
Syntax:fgets(file, length)
![Page 45: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/45.jpg)
Read Single Line - fgets()
$file = fopen("test.txt","r");
echo fgets($file). "<br />";
fclose($file);
![Page 46: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/46.jpg)
Check End-Of-File - feof()
The feof() function checks if the "end-of-file" (EOF) has been reached.
This function returns TRUE if an error occurs, or if EOF has been reached. Otherwise it returns FALSE.
Syntax:feof(file)
![Page 47: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/47.jpg)
Check End-Of-File - feof()
$file = fopen("test.txt", "r");
//Output a line of the file until the end is reachedwhile(! feof($file)) {echo fgets($file). "<br />";}
fclose($file);
![Page 48: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/48.jpg)
Read Single Character - fgetc()
The fgetc() function returns a single character from an open file.
Syntax:fgetc(file)
![Page 49: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/49.jpg)
Read Single Character - fgetc()
$file = fopen("test2.txt", "r");
while (! feof ($file)) {echo fgetc($file);}
fclose($file);
![Page 50: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/50.jpg)
Seek File - fseek()
The fseek() function seeks in an open file.
This function moves the file pointer from its current position to a new position, forward or backward, specified by the number of bytes.
This function returns 0 on success, or -1 on failure. Seeking past EOF will not generate an error.
Syntax:fseek(file, offset, whence)
![Page 51: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/51.jpg)
Seek File - fseek()
$file = fopen("test.txt", "r");
// read first linefgets($file);
// move back to beginning of filefseek($file, 0);
![Page 52: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/52.jpg)
Write File - fwrite()
The fwrite() writes to 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 number of bytes written, or FALSE on failure.
Syntax:fwrite(file, string, length)
![Page 53: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/53.jpg)
Write File - fwrite()
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
![Page 54: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/54.jpg)
Write File - fputs()
The fputs() writes to 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 number of bytes written on success, or FALSE on failure.
Syntax:fputs(file, string, length)
![Page 55: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/55.jpg)
Write File - fputs()
$file = fopen("test.txt","w");
echo fputs($file,"Hello World. Testing!");
fclose($file);
![Page 56: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/56.jpg)
Lock File - flock()
The flock() function locks or releases a file.
This function returns TRUE on success or FALSE on failure.
Syntax:flock(file, lock, block)
![Page 57: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/57.jpg)
Lock File - flock()
$file = fopen("test.txt", "w+");
if (flock($file, LOCK_EX)) {fwrite($file, "Write something");flock($file, LOCK_UN);} else {echo "Error locking file!";}
fclose($file);
![Page 58: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/58.jpg)
Working with Directories
• Create directory - mkdir()• Remove directory - rmdir()• Open directory - opendir()• Read directory - readdir()
![Page 59: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/59.jpg)
Create directory - mkdir()
The mkdir() function creates a directory.
This function returns TRUE on success, or FALSE on failure.
Syntax:mkdir(path, mode, recursive, context)
![Page 60: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/60.jpg)
Create directory - mkdir()
mkdir("testing", 0775);
![Page 61: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/61.jpg)
Remove directory - rmdir()
The rmdir() function removes an empty directory.
This function returns TRUE on success, or FALSE on failure.
Syntax:rmdir(dir, context)
![Page 62: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/62.jpg)
Remove directory - rmdir()
$path = "images";
if(!rmdir($path)) {echo ("Could not remove $path");}
![Page 63: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/63.jpg)
Open directory - opendir()
The opendir() function opens a directory handle.
Syntax:opendir(path, context);
![Page 64: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/64.jpg)
Open directory - opendir()
$dir = "images";
if ($dh = opendir($dir)){echo "$dir directory opened";}
closedir($dh);
![Page 65: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/65.jpg)
Read directory - readdir()
The readdir() function returns the name of the next entry in a directory.
Syntax:readdir(dir_handle);
![Page 66: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/66.jpg)
Read directory - readdir()
$dir = "images";
// Open a directory, and read its contentsif (is_dir($dir)){ if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false){ echo "filename:" . $file . "<br>"; } closedir($dh); }}
![Page 67: DIWE - File handling with PHP](https://reader035.fdocuments.net/reader035/viewer/2022081517/58a8855a1a28abb47a8b5f77/html5/thumbnails/67.jpg)
The End
http://twitter.com/rasansmn