05 File Handling Upload Mysql

50 %
50 %
Information about 05 File Handling Upload Mysql
Technology

Published on June 27, 2009

Author: geshan

Source: slideshare.net

Description

File Handing in PHP basics

PHP Day 05 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com http://www.php.net

PHP File Handling Funcitons Fopen(“pathoileile_name.ext”, “mode”); Returns a resource. $handle = fopen("c:atanfo.txt", "r");

Fopen(“pathoileile_name.ext”, “mode”);

Returns a resource.

$handle = fopen("c:atanfo.txt", "r");

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

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

Simple file read <?php $file_to_operate = fopen(&quot;textfile.txt&quot;,&quot;r&quot;); while(!feof($file_to_operate)){ $a_line = fgets($file_to_operate, 40); //assume all lines have 40 or less characters echo &quot;<br>&quot;.$a_line; } ?> code at day05prog42_file_handling.php

<?php

$file_to_operate = fopen(&quot;textfile.txt&quot;,&quot;r&quot;);

while(!feof($file_to_operate)){

$a_line = fgets($file_to_operate, 40);

//assume all lines have 40 or less characters

echo &quot;<br>&quot;.$a_line;

}

?> code at day05prog42_file_handling.php

Write to a file then read from it <?php $filename = &quot;myfile.txt&quot;; $handle = fopen($filename, &quot;w&quot;); if(!$handle){ print (&quot;<br>Error, &quot;); print (&quot;$filename could not be created.&quot;); die (&quot;Check write properties in folder.&quot;); } for($i=1;$i<=5;$i++){ fputs($handle, &quot;Writing to file at line $i. &quot;); } fclose($handle); //continued in next slide

<?php

$filename = &quot;myfile.txt&quot;;

$handle = fopen($filename, &quot;w&quot;);

if(!$handle){

print (&quot;<br>Error, &quot;);

print (&quot;$filename could not be created.&quot;);

die (&quot;Check write properties in folder.&quot;);

}

for($i=1;$i<=5;$i++){

fputs($handle, &quot;Writing to file at line $i. &quot;);

}

fclose($handle); //continued in next slide

Reading part after writing //now reading from the file just written on $handle_r = fopen ($filename, &quot;r&quot;); if(!$handle_r){ print (&quot;<br>Error, &quot;); print (&quot;$filename could not be read.&quot;); exit(); } while(!feof($handle_r)){ $line_print = fgets($handle_r, 250); //print (&quot;$line_print<br> &quot;); print nl2br($line_print); } fclose($handle_r); ?>

//now reading from the file just written on

$handle_r = fopen ($filename, &quot;r&quot;);

if(!$handle_r){

print (&quot;<br>Error, &quot;);

print (&quot;$filename could not be read.&quot;);

exit();

}

while(!feof($handle_r)){

$line_print = fgets($handle_r, 250);

//print (&quot;$line_print<br> &quot;);

print nl2br($line_print);

}

fclose($handle_r);

?>

File Upload Form <form enctype=&quot;multipart/form-data&quot; action=&quot;file_upload_process.php&quot; method=&quot;POST&quot;> <table name=&quot;file_upload&quot;> <tr> <td>Select file: </td> <td><input name=&quot;userfile&quot; type=&quot; file &quot; /></td> </tr> <tr> <td></td> <td> <input type=&quot;submit&quot; value=&quot;Send File&quot; /> </td> </tr> </table> </form>

<form enctype=&quot;multipart/form-data&quot; action=&quot;file_upload_process.php&quot; method=&quot;POST&quot;>

<table name=&quot;file_upload&quot;>

<tr>

<td>Select file: </td>

<td><input name=&quot;userfile&quot; type=&quot; file &quot; /></td>

</tr>

<tr>

<td></td>

<td>

<input type=&quot;submit&quot; value=&quot;Send File&quot; />

</td>

</tr>

</table>

</form>

$_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 &quot;image/gif&quot;. This mime type is however not checked on the PHP side and therefore don't take its value for granted.

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 &quot;image/gif&quot;. This mime type is however not checked on the PHP side and therefore don't take its value for granted.

$_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

$_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

File Upload Form Example <form enctype=&quot;multipart/form-data&quot; action=&quot;file_upload_process.php&quot; method=&quot;POST&quot;> <table name=&quot;file_upload&quot;> <tr> <td>Select file: </td> <td><input name=&quot;userfile&quot; type=&quot; file &quot; /></td> </tr> <tr> <td></td> <td> <input type=&quot;submit&quot; value=&quot;Send File&quot; /> </td> </tr> </table> </form>

<form enctype=&quot;multipart/form-data&quot; action=&quot;file_upload_process.php&quot; method=&quot;POST&quot;>

<table name=&quot;file_upload&quot;>

<tr>

<td>Select file: </td>

<td><input name=&quot;userfile&quot; type=&quot; file &quot; /></td>

</tr>

<tr>

<td></td>

<td>

<input type=&quot;submit&quot; value=&quot;Send File&quot; />

</td>

</tr>

</table>

</form>

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 &quot;File is valid, and was successfully uploaded. &quot;; } else { echo &quot;Possible file upload attack! &quot;; } print &quot;</pre>&quot;; ?> Code at: day05file_upload_process.php

<?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 &quot;File is valid, and was successfully uploaded. &quot;;

} else {

echo &quot;Possible file upload attack! &quot;;

}

print &quot;</pre>&quot;;

?>

Code at: day05file_upload_process.php

A Form code with all elements Form Code at day05prog46_test_form.php

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 &quot;<h3>Data Got from the previous form:</h3>&quot;; foreach($fdata as $key => $value){ print &quot;<br>&quot;.$key.&quot; - Has ---------------> &quot;.$value; }

$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 &quot;<h3>Data Got from the previous form:</h3>&quot;;

foreach($fdata as $key => $value){

print &quot;<br>&quot;.$key.&quot; - Has ---------------> &quot;.$value;

}

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.

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.

MYSQL Features Multiple storage engines (MyISAM, InnoDB…) Views creation and update Transactions with the InnoDB Engine Sub Queries / Nested Select Primary key and indexing

Multiple storage engines (MyISAM, InnoDB…)

Views creation and update

Transactions with the InnoDB Engine

Sub Queries / Nested Select

Primary key and indexing

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.

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.

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.

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.

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.

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.

DBDesigner 4

PHPMyAdmin

Questions???

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).

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).

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.

Using DB Designer 4 lets sketch the database for a login system.

Some MYSQL user management.

Then insert some users with use of PHPMyAdmin.

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

PHP: Handling file uploads - Manual

Read more

PHP 5 File Upload - W3Schools Online Web Tutorials

PHP Arrays Multi PHP Date and Time PHP Include PHP File Handling PHP File Open ... MySQL Database MySQL ... Without the requirements above, the file upload ...
Read more

PHP and MySQL Programming/File Handling - Wikibooks, open ...

PHP and MySQL Programming/File Handling. ... PHP and MySQL Programming; Navigation menu. ... Upload file; Special pages;
Read more

How To Handle File Uploads With PHP - SitePoint – Learn ...

A common challenge faced by PHP programmers is how to accept files uploaded by ... Using PHP & MySQL ... responsible for handling the form ...
Read more

PHP/MySQL/Linux: File upload, database import and access ...

... File upload, database import and access ... with PHP's "move_uploaded_file" can ... loading of the file contents to a database via PHP/MySQL.
Read more

PHP MySQL - File Upload and MySQL Database - YouTube

Demonstrates how to construct an html form for file upload, how to setup a php script to handle the file upload, save the file to a directory on ...
Read more

PHP 5 File Create/Write - W3Schools Online Web Tutorials

... File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Error Handling PHP Exception MySQL ... File Create ...
Read more

MySQL

MySQL in SaaS, Part II: Effective Monitoring for increased Performance, Security & Uptime Tuesday, February 23, 2016 ; MySQL Jumpstart Tuesday, March 22 ...
Read more

phpmaster | Uploading Files with PHP - SitePoint – Learn ...

Learn how you how you can support uploading files ... the script responsible for handling the upload uses the information found in the $_FILES array ...
Read more