The document discusses various PHP file operations and functions. It covers: - Creating and opening files with fopen(), specifying read/write modes - Reading from files with fgets() - Writing to files with fwrite() - Removing files with unlink() - Appending data to files - Locking files during reads/writes with flock() - Uploading files via HTML forms by accessing the $_FILES array - Getting file information and performing directory operations
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);
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>
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
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();
#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.