Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "More on PHP Coding Lab no. 6 Advance Database Management System."— 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) Home Tutorials References Examples About Us Contact Us

4 Example of Include() All pages in the Web site should include this menu file. Here is how it can be done: Welcome to my home page. Some text.

5 If you look at the source code of the page above (in a browser), it will look like this: Home Tutorials References Examples About Us Contact Us Welcome to my home page! Some text.

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 "; echo date("Y.m.d"). " "; echo date("Y-m-d") ?> Output – 2009/10/14 2009.10.14 2009-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

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:

12 Closing a File fclose() – Closes an open file Example –

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: "; } 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:

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 –

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 – "; echo "Last access: ".date("F d Y H:i:s.",fileatime("test.txt")); ?> filectime() – Returns last change time of file – "; 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 –

20 Locking Files 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. Optional. Set to 1 to block other processes while locking

21

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

23 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 Uploading a File through PHP Form --> File to Upload: 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

24 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']." \n"; print "name: ".$file_array['name']." \n"; print "type: ".$file_array['type']." \n"; print "size: ".$file_array['size']." \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! "; } ?> 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


Download ppt "More on PHP Coding Lab no. 6 Advance Database Management System."

Similar presentations


Ads by Google