8/14/2019 05 File Handling Upload MYSQL
1/23
GeshanManandhar.comGeshanManandhar.com 11
PHP Day 05PHP Day 05
Geshan ManandharGeshan ManandharDeveloper,Developer,
Young Innovations Private LimitedYoung Innovations Private Limited
www.geshanmanandhar.comwww.geshanmanandhar.com
http://www.php.net
8/14/2019 05 File Handling Upload MYSQL
2/23
GeshanManandhar.com 2
PHP File Handling FuncitonsPHP File Handling Funcitons
Fopen(path\\to\\file\\file_name.ext, mode);Fopen(path\\to\\file\\file_name.ext, mode);
Returns a resource.Returns a resource.
$handle = fopen("c:\\data\\info.txt", "r");$handle = fopen("c:\\data\\info.txt", "r");
8/14/2019 05 File Handling Upload MYSQL
3/23
GeshanManandhar.com 3
Other PHP file relatedOther PHP file related
FunctionsFunctions$handle = fopen(test.txt, w+);
fgetsfgets ( resource $handle [, int $length] );( resource $handle [, int $length] );
fwritefwrite( resource $handle, string $string );( resource $handle, string $string );
feoffeof( resource $handle );( resource $handle );fclosefclose ( resource $handle );( resource $handle );
file_existsfile_exists ( string $filename_with_path );( string $filename_with_path );
8/14/2019 05 File Handling Upload MYSQL
4/23
GeshanManandhar.com 4
Simple file readSimple file read
?> code at day05\prog42_file_handling.phpcode at day05\prog42_file_handling.php
8/14/2019 05 File Handling Upload MYSQL
5/23
GeshanManandhar.com 5
Write to a file then read fromWrite to a file then read from
itit
8/14/2019 05 File Handling Upload MYSQL
6/23
GeshanManandhar.com 6
Reading part after writingReading part after writing
//now reading from the file just written on//now reading from the file just written on
$handle_r = fopen ($filename, "r");$handle_r = fopen ($filename, "r");
if(!$handle_r){if(!$handle_r){print ("
Error, ");print ("
Error, ");
print ("$filename could not be read.");print ("$filename could not be read.");
exit();exit();
}}
while(!feof($handle_r)){while(!feof($handle_r)){
$line_print = fgets($handle_r, 250);$line_print = fgets($handle_r, 250);
//print ("$line_print
\n");//print ("$line_print
\n");
print nl2br($line_print);print nl2br($line_print);
}}
fclose($handle_r);fclose($handle_r);
?>?>
8/14/2019 05 File Handling Upload MYSQL
7/23
GeshanManandhar.com 7
File UploadFile Upload FormForm
Select file: Select file:
8/14/2019 05 File Handling Upload MYSQL
8/23
8/14/2019 05 File Handling Upload MYSQL
9/23
GeshanManandhar.com 9
$_FILES Array$_FILES Array
$_FILES['file_field_name']['size']$_FILES['file_field_name']['size'] The size, in bytes, of the uploaded file.The size, in bytes, of the uploaded file.
$_FILES['file_field_name']['tmp_name']$_FILES['file_field_name']['tmp_name'] The temporary filename of the file in which theThe temporary filename of the file in which the
uploaded file was stored on the server.uploaded file was stored on the server.
$_FILES['file_field_name']['error']$_FILES['file_field_name']['error'] The error code associated with this file upload. ThisThe error code associated with this file upload. This
element was added in PHP 4.2.0element was added in PHP 4.2.0
8/14/2019 05 File Handling Upload MYSQL
10/23
GeshanManandhar.com 10
File UploadFile Upload Form ExampleForm Example
Select file: Select file:
8/14/2019 05 File Handling Upload MYSQL
11/23
GeshanManandhar.com 11
File Upload ProcessFile Upload Process
?>Code at: day05\file_upload_process.phpCode at: day05\file_upload_process.php
8/14/2019 05 File Handling Upload MYSQL
12/23
GeshanManandhar.com 12
A Form code with allA Form code with all
elementselements
Form Code at
day05\prog46_test_form.php
8/14/2019 05 File Handling Upload MYSQL
13/23
GeshanManandhar.com 13
Just displaying what it throwsJust displaying what it throws
$fdata['user_login'] = $_POST['user_login'];$fdata['user_login'] = $_POST['user_login'];
$fdata['pass_word'] = $_POST['pass_word'];$fdata['pass_word'] = $_POST['pass_word'];
$fdata['address'] = $_POST['address'];$fdata['address'] = $_POST['address'];
$fdata['email'] = $_POST['email'];$fdata['email'] = $_POST['email'];
$fdata['gender'] = $_POST['gender'];$fdata['gender'] = $_POST['gender'];$fdata['heard_from'] = $_POST['heard_from'];$fdata['heard_from'] = $_POST['heard_from'];
$fdata['newsletter'] = $_POST['newsletter'];$fdata['newsletter'] = $_POST['newsletter'];
print "Data Got from the previous form:";print "Data Got from the previous form:";
foreach($fdata as $key => $value){foreach($fdata as $key
=> $value){print "
".$key." - Has ---------------> ".$value;print "
".$key." - Has ---------------> ".$value;
}}
8/14/2019 05 File Handling Upload MYSQL
14/23
GeshanManandhar.com 14
MYSQLMYSQL
MYSQL is a free and open source relationalMYSQL is a free and open source relationaldatabase management system.database management system.
MYSQL has more than 11 million installations.MYSQL has more than 11 million installations.
MYSQL runs as a server providing multi-userMYSQL runs as a server providing multi-useraccess to a number of databases.access to a number of databases.
It is a cross platform database server.It is a cross platform database server.
MySQL 5.x has many added features.MySQL 5.x has many added features.
8/14/2019 05 File Handling Upload MYSQL
15/23
GeshanManandhar.com 15
MYSQL FeaturesMYSQL Features
Multiple storage engines (MyISAM, InnoDB)Multiple storage engines (MyISAM, InnoDB)
Views creation and updateViews creation and update
Transactions with the InnoDB EngineTransactions with the InnoDB Engine
Sub Queries / Nested SelectSub Queries / Nested Select
Primary key and indexingPrimary key and indexing
8/14/2019 05 File Handling Upload MYSQL
16/23
GeshanManandhar.com 16
MYSQL data types/field typesMYSQL data types/field types
char( length ) fixed lengthchar( length ) fixed length
varchar( 0-255 ) - variable length,varchar( 0-255 ) - variable length,occupies space as per the length of data.occupies space as per the length of data.
Int( ) signed and unsigned values,Int( ) signed and unsigned values,
unsigned holds values from 0 tounsigned holds values from 0 to4294967295.4294967295.
Text holds data character up to 65536Text holds data character up to 65536characters.characters.
8/14/2019 05 File Handling Upload MYSQL
17/23
GeshanManandhar.com 17
MYSQL data types/field typesMYSQL data types/field types
Float floating point numbers has singleFloat floating point numbers has singleprecision. Allowable values areprecision. Allowable values are-3.402823466E+38 to -1.175494351E-38, 0,-3.402823466E+38 to -1.175494351E-38, 0,and 1.175494351E-38 to 3.402823466E+38and 1.175494351E-38 to 3.402823466E+38
Datetime for time stamps formatDatetime for time stamps formatYYYY:MM:DD HH:MM:SS (date and time alsoYYYY:MM:DD HH:MM:SS (date and time alsopossible separately)possible separately)
ENUM(Option1, Option2, Option n) ENUM(Option1, Option2, Option n) for per specified fixed options like eye colorfor per specified fixed options like eye colorcan be black, brown, hazel, green only.can be black, brown, hazel, green only.
8/14/2019 05 File Handling Upload MYSQL
18/23
GeshanManandhar.com 18
Tools to assist MYSQLTools to assist MYSQL
developmentdevelopmentDBDesigner 4DBDesigner 4 is a free available database designis a free available database design
system that integrates database design,system that integrates database design,modeling, creation and maintenance into amodeling, creation and maintenance into asingle, seamless environment. Download itsingle, seamless environment. Download it herehere..
PHPMyAdminPHPMyAdmin is an open source tool written inis an open source tool written inPHP intended to handle the administration ofPHP intended to handle the administration ofMYSQL over the World Wide Web. ComesMYSQL over the World Wide Web. Comesbundled with XAMPP.bundled with XAMPP.
http://www.fabforce.net/downloads.phphttp://www.fabforce.net/downloads.phphttp://www.fabforce.net/downloads.php8/14/2019 05 File Handling Upload MYSQL
19/23
GeshanManandhar.com 19
DBDesigner 4DBDesigner 4
8/14/2019 05 File Handling Upload MYSQL
20/23
GeshanManandhar.com 20
PHPMyAdminPHPMyAdmin
8/14/2019 05 File Handling Upload MYSQL
21/23
GeshanManandhar.com 21
Questions???Questions???
8/14/2019 05 File Handling Upload MYSQL
22/23
GeshanManandhar.com 22
AssignmentAssignment
Write a string taken input from a formWrite a string taken input from a formto a file called user_input.txt and showto a file called user_input.txt and show
it after reading from the same file.it after reading from the same file.
Create a user registration form withCreate a user registration form with
picture upload of just .jpg type and filepicture upload of just .jpg type and filesize less than 60 kb.size less than 60 kb. (let it be(let it be
accessible only after logging in to youraccessible only after logging in to yourlogin system you created).login system you created).
8/14/2019 05 File Handling Upload MYSQL
23/23
GeshanManandhar.com 23
Lets start some Db designLets start some Db design
Using DB Designer 4 lets sketch theUsing DB Designer 4 lets sketch thedatabase for a login system.database for a login system.
Some MYSQL user management.Some MYSQL user management.
Then insert some users with use ofThen insert some users with use of
PHPMyAdmin.PHPMyAdmin.
Top Related