Php File Operations

2,430 views

Published on

PHP file operations

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,430
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
52
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • fgets(file,length):file: Required. Specifies the file to read fromLength:Optional. Specifies the number of bytes to read. Default is 1024 bytes.
  • 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.
  • Php File Operations

    1. 1. PHP File OperationsJamshid HashimiTrainer, Cresco Solutionhttp://www.jamshidhashimi.comjamshid@netlinks.af@jamshidhashimiajamshidhashimiAfghanistan WorkforceDevelopment Program
    2. 2. 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()
    3. 3. Agenda• Uploading Files via an HTML Form• Getting File Information• More File Functions• Directory Functions• Getting a Directory Listing
    4. 4. Introduction• Manipulating files is a basic necessity forserious programmers and PHP gives you agreat deal of tools for creating, uploading, andediting files.• This is one of the most fundamental subjectsof server side programming in general. Filesare used in web applications of all sizes.
    5. 5. Creating a File• In PHP the fopen function is used to openfiles. However, it can also create a file if it doesnot find the file specified in the function call.So if you use fopen on a file that does notexist, it will create it, given that you open thefile for writing or appending.$ourFileName = "testFile.txt";$ourFileHandle = fopen($ourFileName, w)or die("cant open file");fclose($ourFileHandle);
    6. 6. File Operations Mode
    7. 7. Opening a File• The fopen() function is used to open files inPHP.<?php$file = fopen("welcome.txt", "r") or exit("Unable toopen file!");fclose($file);
    8. 8. Reading From a File• The fgets() function is used to read a singleline from a file.<?php$file = fopen("welcome.txt", "r") or exit("Unable toopen file!");//Output a line of the file until the end is reachedwhile(!feof($file)){echo fgets($file). "<br>";}fclose($file);
    9. 9. Writing to a File• We can use php to write to a text file. Thefwrite function allows data to be written toany type of file.$myFile = "testFile.txt";$fh = fopen($myFile, w) or die("cantopen file");$stringData = "Kabul is the capitaln";fwrite($fh, $stringData);$stringData = "Samangan is a provincen";fwrite($fh, $stringData);fclose($fh);
    10. 10. Removing File• In PHP you delete files by calling the unlinkfunction.$myFile = "testFile.txt";unlink($myFile);
    11. 11. Appending Data$myFile = "testFile.txt";$fh = fopen($myFile, a) or die("cant open file");$stringData = "New Stuff 1n";fwrite($fh, $stringData);$stringData = "New Stuff 2n";fwrite($fh, $stringData);fclose($fh);
    12. 12. File Locking• The key problem with file system operations isthe situation you are in if two scripts attemptto write to a file at the same time.• The fopen() function, when called on afile, does not stop that same file from beingopened by another script.
    13. 13. 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 orexclusive).$fp = fopen( $filename,"w"); // open it forWRITING ("w")if (flock($fp, LOCK_EX)) {// do your file writes hereflock($fp, LOCK_UN); // unlock the file} else {// flock() returned false, no lockobtainedprint "Could not lock $filename!n";}
    14. 14. Uploading Files via an HTML Form• With PHP, it is possible to upload files to theserver.<!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. 15. Uploading Files via an HTML Form<?phpif ($_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. 16. 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_filesizepost_max_size = 40M
    17. 17. Directory Functions• scandir()• getcwd()<?phpprint_r(scandir(”projects"));?><?phpecho getcwd();?>
    18. 18. Directory Functions• chdir()– The chdir() function changes the current directoryto the specified directory.<?php//Get current directoryecho getcwd();echo "<br />";//Change to the images directorychdir(”projects");echo "<br />";echo getcwd();
    19. 19. DEMO
    20. 20. QUESTIONS?

    ×