You are on page 1of 23

PHP Day 05

http://www.php.net

Geshan Manandhar
Developer,
Young Innovations Private Limited
www.geshanmanandhar.com
GeshanManandhar.com 1
PHP File Handling Funcitons
► Fopen(“path\\to\\file\\file_name.ext”, “mode”);
► Returns a resource.
► $handle = fopen("c:\\data\\info.txt", "r");

GeshanManandhar.com 2
Other PHP file related
Functions
► $handle = fopen(“test.txt”, “w+”);
► fgets ( resource $handle [, int $length] );
► fwrite( resource $handle, string $string );
► feof ( resource $handle );
► fclose ( resource $handle );
► file_exists ( string $filename_with_path );

GeshanManandhar.com 3
Simple file read
<?php
$file_to_operate = fopen("textfile.txt","r");

while(!feof($file_to_operate)){
$a_line = fgets($file_to_operate, 40);
//assume all lines have 40 or less characters
echo "<br>".$a_line;
}
?> code at day05\prog42_file_handling.php
GeshanManandhar.com 4
Write to a file then read from
it
<?php
$filename = "myfile.txt";
$handle = fopen($filename, "w");

if(!$handle){
print ("<br>Error, ");
print ("$filename could not be created.");
die ("Check write properties in folder.");
}

for($i=1;$i<=5;$i++){
fputs($handle, "Writing to file at line $i.\n");
}
fclose($handle); //continued in next slide

GeshanManandhar.com 5
Reading part after writing
//now reading from the file just written on

$handle_r = fopen ($filename, "r");


if(!$handle_r){
print ("<br>Error, ");
print ("$filename could not be read.");
exit();
}

while(!feof($handle_r)){
$line_print = fgets($handle_r, 250);
//print ("$line_print<br>\n");
print nl2br($line_print);
}

fclose($handle_r);
?>
GeshanManandhar.com 6
File Upload Form
<form enctype="multipart/form-data"
action="file_upload_process.php" method="POST">
<table name="file_upload">
<tr>
<td>Select file: </td>
<td><input name="userfile" type="file" /></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Send File" />
</td>
</tr>
</table>
</form>
GeshanManandhar.com 7
$_FILES Array
► An associative array of items uploaded to the
current script via the HTTP POST method.
► $_FILES['file_field_name']['name']
 The original name of the file on the client machine.

► $_FILES['file_field_name']['type']
 The mime type of the file, if the browser provided this
information. An example would be "image/gif". This
mime type is however not checked on the PHP side
and therefore don't take its value for granted.

GeshanManandhar.com 8
$_FILES Array
► $_FILES['file_field_name']['size']
 The size, in bytes, of the uploaded file.

► $_FILES['file_field_name']['tmp_name']
 The temporary filename of the file in which the
uploaded file was stored on the server.

► $_FILES['file_field_name']['error']
 The error code associated with this file upload. This
element was added in PHP 4.2.0

GeshanManandhar.com 9
File Upload Form Example
<form enctype="multipart/form-data"
action="file_upload_process.php" method="POST">
<table name="file_upload">
<tr>
<td>Select file: </td>
<td><input name="userfile" type="file" /></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Send File" />
</td>
</tr>
</table>
</form>
GeshanManandhar.com 10
File Upload Process
<?php
$uploaddir = 'uploads/'; //relative path to where this file is
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}

print "</pre>";

?>
Code at: day05\file_upload_process.php

GeshanManandhar.com 11
A Form code with all
elements

Form Code at
day05\prog46_test_form.php
GeshanManandhar.com 12
Just displaying what it throws
$fdata['user_login'] = $_POST['user_login'];
$fdata['pass_word'] = $_POST['pass_word'];
$fdata['address'] = $_POST['address'];
$fdata['email'] = $_POST['email'];
$fdata['gender'] = $_POST['gender'];
$fdata['heard_from'] = $_POST['heard_from'];
$fdata['newsletter'] = $_POST['newsletter'];

print "<h3>Data Got from the previous form:</h3>";


foreach($fdata as $key => $value){
print "<br>".$key." - Has ---------------> ".$value;
}

GeshanManandhar.com 13
MYSQL
► MYSQL is a free and open source relational
database management system.
► MYSQL has more than 11 million installations.
► MYSQL runs as a server providing multi-user
access to a number of databases.
► It is a cross platform database server.
► MySQL 5.x has many added features.

GeshanManandhar.com 14
MYSQL Features
► Multiplestorage engines (MyISAM, InnoDB…)
► Views creation and update
► Transactions with the InnoDB Engine
► Sub Queries / Nested Select
► Primary key and indexing

GeshanManandhar.com 15
MYSQL data types/field types
► char( length ) – fixed length
► varchar( 0-255 ) - variable length,
occupies space as per the length of data.
► Int( ) – signed and unsigned values,
unsigned holds values from 0 to
4294967295.
► Text – holds data character up to 65536
characters.

GeshanManandhar.com 16
MYSQL data types/field types
► Float – floating point numbers has single
precision. Allowable values are
-3.402823466E+38 to -1.175494351E-38, 0,
and 1.175494351E-38 to 3.402823466E+38
► Datetime – for time stamps format
YYYY:MM:DD HH:MM:SS (date and time also
possible separately)
► ENUM(‘Option1’, ‘Option2’, … ‘Option n’) –
for per specified fixed options like eye color
can be black, brown, hazel, green only.

GeshanManandhar.com 17
Tools to assist MYSQL
development
► DBDesigner 4 is a free available database design
system that integrates database design,
modeling, creation and maintenance into a
single, seamless environment. Download it here.
► PHPMyAdmin is an open source tool written in
PHP intended to handle the administration of
MYSQL over the World Wide Web. Comes
bundled with XAMPP.

GeshanManandhar.com 18
DBDesigner 4

GeshanManandhar.com 19
PHPMyAdmin

GeshanManandhar.com 20
Questions???

GeshanManandhar.com 21
Assignment
► Write a string taken input from a form
to a file called user_input.txt and show
it after reading from the same file.
► Create a user registration form with
picture upload of just .jpg type and file
size less than 60 kb. (let it be
accessible only after logging in to your
login system you created).

GeshanManandhar.com 22
Lets start some Db design
► Using DB Designer 4 lets sketch the
database for a login system.
► Some MYSQL user management.
► Then insert some users with use of
PHPMyAdmin.

GeshanManandhar.com 23

You might also like