1 / 23

More on PHP Coding

More on PHP Coding. Lab no. 6 Advance Database Management System. Lab Outline. An example of Include() related to previous Lab PHP Date Function File Handling functions in PHP. Example of Include(). Assume we have a standard menu file ("menu.php“) that should be used on all pages

jock
Download Presentation

More on PHP Coding

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on PHP Coding Lab no. 6 Advance Database Management System

  2. Lab Outline • An example of Include() • related to previous Lab • PHP Date Function • File Handling functions in PHP

  3. Example of Include() • Assume we have a standard menu file ("menu.php“) that should be used on all pages • Menu.php (snippet) <a href="/default.php">Home</a><a href="/tutorials.php">Tutorials</a><a href="/references.php">References</a><a href="/examples.php">Examples</a> <a href="/about.php">About Us</a> <a href="/contact.php">Contact Us</a>

  4. Example of Include() • All pages in the Web site should include this menu file. Here is how it can be done: <html><body><div class="leftmenu"><?php include("menu.php"); ?></div><h1>Welcome to my home page.</h1><p>Some text.</p></body></html>

  5. If you look at the source code of the page above (in a browser), it will look like this: • <html><body><div class="leftmenu"><a href="/default.php">Home</a><a href="/tutorials.php">Tutorials</a><a href="/references.php">References</a><a href="/examples.php">Examples</a> <a href="/about.php">About Us</a> <a href="/contact.php">Contact Us</a></div><h1>Welcome to my home page!</h1><p>Some text.</p></body></html>

  6. PHP Date() • date(format,timestamp) • format • Required. Specifies the format of the date/time • timestamp • Optional. Specifies a timestamp. Default is the current date and time • Characters used in format parameter • d - Represents the day of the month (01 to 31) • m - Represents a month (01 to 12) • Y - Represents a year (in four digits)

  7. <?phpecho date("Y/m/d") . "<br />";echo date("Y.m.d") . "<br />";echo date("Y-m-d")?> • Output • 2009/10/142009.10.142009-10-14 • For complete reference on PHP Date/Time functions • http://www.w3schools.com/php/php_ref_date.asp

  8. PHP File Handling

  9. Opening a File • fopen() • This function is used to open files in PHP. • first parameter of this function contains the name of the file to be opened • second parameter specifies in which mode the file should be opened: • Example <html><body><?php$file=fopen("welcome.txt","r");?></body></html>

  10. File opening modes Note: If the fopen() function is unable to open the specified file, it returns 0 (false).

  11. Another Example • The following example generates a message if the fopen() function is unable to open the specified file: • <html><body><?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");?></body></html>

  12. Closing a File • fclose() • Closes an open file • Example • <?php$file = fopen("test.txt","r");//some code to be executedfclose($file);?>

  13. End-of-File Check • feof() • checks if the "end-of-file" (EOF) has been reached. • This function is useful for looping through data of unknown length. • Note: You cannot read from files opened in w, a, and x mode! • Example code snippet • if (feof($file)) echo "End of file";

  14. Line by Line Reading from File • fgets() • It is used to read a single line from a file. • Note: After a call to this function the file pointer has moved to the next line. • Example: Following code reads a file line by line, until the end of file is reached: • <?php$file = fopen("welcome.txt", "r") or exit("Unable to open file!");//Output a line of the file until the end is reachedwhile(!feof($file))  {  echo fgets($file). "<br />";  }fclose($file);?>

  15. Reading a File – Character by Character • fgetc() • It is used to read a single character from a file. • Note: After a call to this function the file pointer moves to the next character. • Example: The example below reads a file character by character, until the end of file is reached: • <?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");while (!feof($file))  {  echo fgetc($file);  }fclose($file);?>

  16. More PHP File Functions • file_exists() • Checks for existence of file • if (file_exists("test.txt")) { print "The file exists!"; } • is_dir() • Checks whether a file is a directory. • is_dir(“path”); requires the file path and returns a Boolean value. • filesize() • returns the file size in bytes on success or FALSE on failure • <?phpecho filesize("test.txt");?>

  17. More PHP File Functions • is_readable() • Checks whether a file is readable • if(is_readable(“test.txt”))   {  echo (“file is readable");  } • is_writable() • Checks whether file is writable. alias function is_writeable() • if(is_writable($file))   {   echo ("$file is writeable");   } • is_executable() • checks whether you can run a file, relying on either the file's permissions or its extension depending on your platform. • accepts the file path and returns a Boolean value. • if(is_executable($file))   {   echo ("$file is executable");   }

  18. More PHP File Functions • fileatime() • Returns the last access time of file • <?phpecho fileatime("test.txt");echo "<br />";echo "Last access: ".date("F d Y H:i:s.",fileatime("test.txt"));?> • filectime() • Returns last change time of file • <?phpecho filectime("test.txt");echo "<br />";echo "Last change: ".date("F d Y H:i:s.",filectime("test.txt"));?>

  19. More PHP File Functions • fwrite() • Alias of fwrite is fputs() function • Writes to an open file • <?php$file = fopen("test.txt","w");echo fwrite($file,"Hello World. Testing!");fclose($file);?>

  20. Locking Files Optional. Set to 1 to block other processes while locking • flock() • Locks or releases a file • flock(filename,lockConstant,block) You should call flock() directly after calling fopen() call it again to release the lock before closing the file. The lock is released also by fclose(), which is called automatically when script is finished.

  21. Working with File Uploads By using the global PHP $_FILES array you can upload files from a client computer to the remote server.

  22. Uploading a File through PHP Form specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded • <html><body><form action=“Upload_file.php" enctype="multipart/form-data" method="POST"> • <!-- <input type="hidden" name="MAX_FILE_SIZE" value="51200">--> • File to Upload: <input type="file" name="fileup"><br><br> • <input type="submit" value="upload!"> • </form></body></html> specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field

  23. Upload_file.php • By using the global PHP $_FILES array you can upload files from a client computer to the remote server. <?php $file_dir = "C:/wamp/www/Files"; foreach($_FILES as $file_array) { print "path: ".$file_array['tmp_name']."<br>\n"; print "name: ".$file_array['name']."<br>\n"; print "type: ".$file_array['type']."<br>\n"; print "size: ".$file_array['size']."<br>\n"; if (is_uploaded_file($file_array['tmp_name'])) { move_uploaded_file($file_array['tmp_name'] ,"$file_dir/$file_array[name]") or die ("Couldn't copy"); print "file was moved!<br><br>"; } } ?> the name of the temporary copy of the file stored on the server size in bytes of the uploaded file Saving the uploaded file on a permanent location on server This function accepts a path to an uploaded file and returns true only if the file in question is a valid upload file

More Related