PHP Filing Instructor: Nisa Soomro
Outlines • File manipulation using PHP • Basic protocols • Cookies and Sessions
Introduction • Manipulating files is a basic necessity for serious programmers and PHP gives you a great deal of tools for creating, uploading, and editing files. • This is one of the most fundamental subjects of server side programming in general. Files are used in web applications of all sizes.
Creating a File • In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call. So if you use fopen on a file that does not exist, it will create it, given that you open the file for writing or appending. $ourFileName = "testFile.txt"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle);
File Operations Mode
Opening a File • The fopen() function is used to open files in PHP. <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); fclose($file);
Reading From a File • The fgets() function is used to read a single line from a file. <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); while(!feof($file)) { echo fgets($file). "<br>"; } fclose($file);
Writing to a File • We can use php to write to a text file. The fwrite function allows data to be written to any type of file. $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = “Hello 13BS(CS)"; fwrite($fh, $stringData); fclose($fh);
Removing File • In PHP you delete files by calling the unlink function. $myFile = "testFile.txt"; unlink($myFile);
Appending Data $myFile = "testFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "New Stuff 1n"; fwrite($fh, $stringData); $stringData = "New Stuff 2n"; fwrite($fh, $stringData); fclose($fh);

PHP Filing

  • 1.
  • 2.
    Outlines • Filemanipulation using PHP • Basic protocols • Cookies and Sessions
  • 3.
    Introduction • Manipulatingfiles is a basic necessity for serious programmers and PHP gives you a great deal of tools for creating, uploading, and editing files. • This is one of the most fundamental subjects of server side programming in general. Files are used in web applications of all sizes.
  • 4.
    Creating a File • In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call. So if you use fopen on a file that does not exist, it will create it, given that you open the file for writing or appending. $ourFileName = "testFile.txt"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle);
  • 5.
  • 6.
    Opening a File • The fopen() function is used to open files in PHP. <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); fclose($file);
  • 7.
    Reading From aFile • The fgets() function is used to read a single line from a file. <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); while(!feof($file)) { echo fgets($file). "<br>"; } fclose($file);
  • 8.
    Writing to aFile • We can use php to write to a text file. The fwrite function allows data to be written to any type of file. $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = “Hello 13BS(CS)"; fwrite($fh, $stringData); fclose($fh);
  • 9.
    Removing File •In PHP you delete files by calling the unlink function. $myFile = "testFile.txt"; unlink($myFile);
  • 10.
    Appending Data $myFile= "testFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "New Stuff 1n"; fwrite($fh, $stringData); $stringData = "New Stuff 2n"; fwrite($fh, $stringData); fclose($fh);

Editor's Notes

  • #8 fgets(file,length): file: Required. Specifies the file to read from Length: Optional. Specifies the number of bytes to read. Default is 1024 bytes.
  • #11 The above example may not seem very useful, but appending data onto a file is actually used everyday. Almost all web servers have a log of some sort.