PHP File Operations Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com jamshid@netlinks.af @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program
Agenda • Introduction • Creating a File • Opening a File – fopen() • Reading From a File – fgets() • Writing to File – fwrite() • Removing File – unlink() • Appending Data • File Locking – flock()
Agenda • Uploading Files via an HTML Form • Getting File Information • More File Functions • Directory Functions • Getting a Directory Listing
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!"); //Output a line of the file until the end is reached 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 = "Kabul is the capitaln"; fwrite($fh, $stringData); $stringData = "Samangan is a provincen"; 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);
File Locking • The key problem with file system operations is the situation you are in if two scripts attempt to write to a file at the same time. • The fopen() function, when called on a file, does not stop that same file from being opened by another script.
File Locking • LOCK_SH to acquire a shared lock (reader). • LOCK_EX to acquire an exclusive lock (writer). • LOCK_UN to release a lock (shared or exclusive). $fp = fopen( $filename,"w"); // open it for WRITING ("w") if (flock($fp, LOCK_EX)) { // do your file writes here flock($fp, LOCK_UN); // unlock the file } else { // flock() returned false, no lock obtained print "Could not lock $filename!n"; }
Uploading Files via an HTML Form • With PHP, it is possible to upload files to the server. <!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
Uploading Files via an HTML Form <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; }
Uploading Files via an HTML Form ; Maximum allowed size for uploaded files. upload_max_filesize = 40M ; Must be greater than or equal to upload_max_filesize post_max_size = 40M
Directory Functions • scandir() • getcwd() <?php print_r(scandir(”projects")); ?> <?php echo getcwd(); ?>
Directory Functions • chdir() – The chdir() function changes the current directory to the specified directory. <?php //Get current directory echo getcwd(); echo "<br />"; //Change to the images directory chdir(”projects"); echo "<br />"; echo getcwd();
DEMO
QUESTIONS?

Php File Operations

  • 1.
    PHP File Operations JamshidHashimi Trainer, Cresco Solution http://www.jamshidhashimi.com jamshid@netlinks.af @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program
  • 2.
    Agenda • Introduction • Creatinga File • Opening a File – fopen() • Reading From a File – fgets() • Writing to File – fwrite() • Removing File – unlink() • Appending Data • File Locking – flock()
  • 3.
    Agenda • Uploading Filesvia an HTML Form • Getting File Information • More File Functions • Directory Functions • Getting a Directory Listing
  • 4.
    Introduction • Manipulating filesis 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.
  • 5.
    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);
  • 6.
  • 7.
    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);
  • 8.
    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!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br>"; } fclose($file);
  • 9.
    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 = "Kabul is the capitaln"; fwrite($fh, $stringData); $stringData = "Samangan is a provincen"; fwrite($fh, $stringData); fclose($fh);
  • 10.
    Removing File • InPHP you delete files by calling the unlink function. $myFile = "testFile.txt"; unlink($myFile);
  • 11.
    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);
  • 12.
    File Locking • Thekey problem with file system operations is the situation you are in if two scripts attempt to write to a file at the same time. • The fopen() function, when called on a file, does not stop that same file from being opened by another script.
  • 13.
    File Locking • LOCK_SHto acquire a shared lock (reader). • LOCK_EX to acquire an exclusive lock (writer). • LOCK_UN to release a lock (shared or exclusive). $fp = fopen( $filename,"w"); // open it for WRITING ("w") if (flock($fp, LOCK_EX)) { // do your file writes here flock($fp, LOCK_UN); // unlock the file } else { // flock() returned false, no lock obtained print "Could not lock $filename!n"; }
  • 14.
    Uploading Files viaan HTML Form • With PHP, it is possible to upload files to the server. <!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
  • 15.
    Uploading Files viaan HTML Form <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; }
  • 16.
    Uploading Files viaan HTML Form ; Maximum allowed size for uploaded files. upload_max_filesize = 40M ; Must be greater than or equal to upload_max_filesize post_max_size = 40M
  • 17.
    Directory Functions • scandir() •getcwd() <?php print_r(scandir(”projects")); ?> <?php echo getcwd(); ?>
  • 18.
    Directory Functions • chdir() –The chdir() function changes the current directory to the specified directory. <?php //Get current directory echo getcwd(); echo "<br />"; //Change to the images directory chdir(”projects"); echo "<br />"; echo getcwd();
  • 19.
  • 20.

Editor's Notes

  • #9 fgets(file,length):file: Required. Specifies the file to read fromLength:Optional. Specifies the number of bytes to read. Default is 1024 bytes.
  • #12 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.