+ All Categories
Home > Documents > More on PHP Coding Lab no. 6 Advance Database Management System.

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

Date post: 16-Dec-2015
Category:
Upload: erin-may
View: 222 times
Download: 0 times
Share this document with a friend
23
More on PHP Coding Lab no. 6 Advance Database Management System
Transcript
Page 1: More on PHP Coding Lab no. 6 Advance Database Management System.

More on PHP Coding

Lab no. 6Advance Database Management

System

Page 2: 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

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

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>

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

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>

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

• 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>

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

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)

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

• <?phpecho date("Y/m/d") . "<br />";echo date("Y.m.d") . "<br />";echo date("Y-m-d")?>

• Output– 2009/10/14

2009.10.142009-10-14

• For complete reference on PHP Date/Time functions– http://www.w3schools.com/php/php_ref_date.asp

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

PHP File Handling

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

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>

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

File opening modes

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

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

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>

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

Closing a File

• fclose()– Closes an open file

• Example– <?php

$file = fopen("test.txt","r");

//some code to be executed

fclose($file);?>

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

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

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

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);?>

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

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);?>

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

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");?>

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

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

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

More PHP File Functions• fileatime()

– Returns the last access time of file– <?php

echo 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– <?php

echo filectime("test.txt");echo "<br />";echo "Last change: ".date("F d Y H:i:s.",filectime("test.txt"));?>

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

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);?>

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

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

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

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

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

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

• <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 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

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

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


Recommended