PHP Day 05 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com http://www.php.net
PHP File Handling Funcitons <ul><li>Fopen(“pathoileile_name.ext”, “mode”); </li></ul><ul><li>Returns a resource. </li></ul...
Other PHP file related Functions <ul><li>$handle = fopen(“test.txt”, “w+”); </li></ul><ul><li>fgets  ( resource $handle [,...
Simple file read <ul><li><?php  </li></ul><ul><li>$file_to_operate = fopen(&quot;textfile.txt&quot;,&quot;r&quot;); </li><...
Write to a file then read from it <ul><li><?php  </li></ul><ul><li>$filename = &quot;myfile.txt&quot;; </li></ul><ul><li>$...
Reading part after writing <ul><li>//now reading from the file just written on </li></ul><ul><li>$handle_r = fopen ($filen...
File Upload  Form <ul><li><form  enctype=&quot;multipart/form-data&quot;  action=&quot;file_upload_process.php&quot; metho...
$_FILES Array <ul><li>An associative array of items uploaded to the current script via the HTTP POST method.  </li></ul><u...
$_FILES Array <ul><li>$_FILES['file_field_name']['size'] </li></ul><ul><ul><li>The size, in bytes, of the uploaded file.  ...
File Upload  Form Example <ul><li><form  enctype=&quot;multipart/form-data&quot;  action=&quot;file_upload_process.php&quo...
File Upload Process <ul><li><?php  </li></ul><ul><li>$uploaddir = 'uploads/'; //relative path to where this file is </li><...
A Form code with all elements Form Code at day05prog46_test_form.php
Just displaying what it throws <ul><li>$fdata['user_login'] = $_POST['user_login']; </li></ul><ul><li>$fdata['pass_word'] ...
MYSQL <ul><li>MYSQL is a free and open source relational database management system. </li></ul><ul><li>MYSQL has more than...
MYSQL Features <ul><li>Multiple storage engines (MyISAM, InnoDB…) </li></ul><ul><li>Views creation and update </li></ul><u...
MYSQL data types/field types <ul><li>char( length ) – fixed length </li></ul><ul><li>varchar( 0-255 ) - variable length, o...
MYSQL data types/field types <ul><li>Float – floating point numbers has single precision. Allowable values are -3.40282346...
Tools to assist MYSQL development <ul><li>DBDesigner 4  is a free available database design system that integrates databas...
DBDesigner 4
PHPMyAdmin
Questions???
Assignment  <ul><li>Write a string taken input from a form to a file called user_input.txt and show it after reading from ...
Lets start some Db design <ul><li>Using DB Designer 4 lets sketch the database for a login system. </li></ul><ul><li>Some ...
Upcoming SlideShare
Loading in...5
×

05 File Handling Upload Mysql

2,078

Published on

File Handing in PHP basics

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,078
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

05 File Handling Upload Mysql

  1. 1. PHP Day 05 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com http://www.php.net
  2. 2. PHP File Handling Funcitons <ul><li>Fopen(“pathoileile_name.ext”, “mode”); </li></ul><ul><li>Returns a resource. </li></ul><ul><li>$handle = fopen(&quot;c:atanfo.txt&quot;, &quot;r&quot;); </li></ul>
  3. 3. Other PHP file related Functions <ul><li>$handle = fopen(“test.txt”, “w+”); </li></ul><ul><li>fgets ( resource $handle [, int $length] ); </li></ul><ul><li>fwrite ( resource $handle, string $string ); </li></ul><ul><li>feof ( resource $handle ); </li></ul><ul><li>fclose ( resource $handle ); </li></ul><ul><li>file_exists ( string $filename_with_path ); </li></ul>
  4. 4. Simple file read <ul><li><?php </li></ul><ul><li>$file_to_operate = fopen(&quot;textfile.txt&quot;,&quot;r&quot;); </li></ul><ul><li>while(!feof($file_to_operate)){ </li></ul><ul><li>$a_line = fgets($file_to_operate, 40); </li></ul><ul><li>//assume all lines have 40 or less characters </li></ul><ul><li>echo &quot;<br>&quot;.$a_line; </li></ul><ul><li>} </li></ul><ul><li>?> code at day05prog42_file_handling.php </li></ul>
  5. 5. Write to a file then read from it <ul><li><?php </li></ul><ul><li>$filename = &quot;myfile.txt&quot;; </li></ul><ul><li>$handle = fopen($filename, &quot;w&quot;); </li></ul><ul><li>if(!$handle){ </li></ul><ul><li>print (&quot;<br>Error, &quot;); </li></ul><ul><li>print (&quot;$filename could not be created.&quot;); </li></ul><ul><li>die (&quot;Check write properties in folder.&quot;); </li></ul><ul><li>} </li></ul><ul><li>for($i=1;$i<=5;$i++){ </li></ul><ul><li>fputs($handle, &quot;Writing to file at line $i. &quot;); </li></ul><ul><li>} </li></ul><ul><li>fclose($handle); //continued in next slide </li></ul>
  6. 6. Reading part after writing <ul><li>//now reading from the file just written on </li></ul><ul><li>$handle_r = fopen ($filename, &quot;r&quot;); </li></ul><ul><li>if(!$handle_r){ </li></ul><ul><li>print (&quot;<br>Error, &quot;); </li></ul><ul><li>print (&quot;$filename could not be read.&quot;); </li></ul><ul><li>exit(); </li></ul><ul><li>} </li></ul><ul><li>while(!feof($handle_r)){ </li></ul><ul><li>$line_print = fgets($handle_r, 250); </li></ul><ul><li>//print (&quot;$line_print<br> &quot;); </li></ul><ul><li>print nl2br($line_print); </li></ul><ul><li>} </li></ul><ul><li>fclose($handle_r); </li></ul><ul><li>?> </li></ul>
  7. 7. File Upload Form <ul><li><form enctype=&quot;multipart/form-data&quot; action=&quot;file_upload_process.php&quot; method=&quot;POST&quot;> </li></ul><ul><li><table name=&quot;file_upload&quot;> </li></ul><ul><li><tr> </li></ul><ul><li><td>Select file: </td> </li></ul><ul><li><td><input name=&quot;userfile&quot; type=&quot; file &quot; /></td> </li></ul><ul><li></tr> </li></ul><ul><li><tr> </li></ul><ul><li><td></td> </li></ul><ul><li><td> </li></ul><ul><li><input type=&quot;submit&quot; value=&quot;Send File&quot; /> </li></ul><ul><li></td> </li></ul><ul><li></tr> </li></ul><ul><li></table> </li></ul><ul><li></form> </li></ul>
  8. 8. $_FILES Array <ul><li>An associative array of items uploaded to the current script via the HTTP POST method. </li></ul><ul><li>$_FILES['file_field_name']['name'] </li></ul><ul><ul><li>The original name of the file on the client machine. </li></ul></ul><ul><li>$_FILES['file_field_name']['type'] </li></ul><ul><ul><li>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. </li></ul></ul>
  9. 9. $_FILES Array <ul><li>$_FILES['file_field_name']['size'] </li></ul><ul><ul><li>The size, in bytes, of the uploaded file. </li></ul></ul><ul><li>$_FILES['file_field_name']['tmp_name'] </li></ul><ul><ul><li>The temporary filename of the file in which the uploaded file was stored on the server. </li></ul></ul><ul><li>$_FILES['file_field_name']['error'] </li></ul><ul><ul><li>The error code associated with this file upload. This element was added in PHP 4.2.0 </li></ul></ul>
  10. 10. File Upload Form Example <ul><li><form enctype=&quot;multipart/form-data&quot; action=&quot;file_upload_process.php&quot; method=&quot;POST&quot;> </li></ul><ul><li><table name=&quot;file_upload&quot;> </li></ul><ul><li><tr> </li></ul><ul><li><td>Select file: </td> </li></ul><ul><li><td><input name=&quot;userfile&quot; type=&quot; file &quot; /></td> </li></ul><ul><li></tr> </li></ul><ul><li><tr> </li></ul><ul><li><td></td> </li></ul><ul><li><td> </li></ul><ul><li><input type=&quot;submit&quot; value=&quot;Send File&quot; /> </li></ul><ul><li></td> </li></ul><ul><li></tr> </li></ul><ul><li></table> </li></ul><ul><li></form> </li></ul>
  11. 11. File Upload Process <ul><li><?php </li></ul><ul><li>$uploaddir = 'uploads/'; //relative path to where this file is </li></ul><ul><li>$uploadfile = $uploaddir . basename($_FILES['userfile']['name']); </li></ul><ul><li>echo '<pre>'; </li></ul><ul><li>if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { </li></ul><ul><li>echo &quot;File is valid, and was successfully uploaded. &quot;; </li></ul><ul><li>} else { </li></ul><ul><li>echo &quot;Possible file upload attack! &quot;; </li></ul><ul><li>} </li></ul><ul><li>print &quot;</pre>&quot;; </li></ul><ul><li>?> </li></ul><ul><li>Code at: day05file_upload_process.php </li></ul>
  12. 12. A Form code with all elements Form Code at day05prog46_test_form.php
  13. 13. Just displaying what it throws <ul><li>$fdata['user_login'] = $_POST['user_login']; </li></ul><ul><li>$fdata['pass_word'] = $_POST['pass_word']; </li></ul><ul><li>$fdata['address'] = $_POST['address']; </li></ul><ul><li>$fdata['email'] = $_POST['email']; </li></ul><ul><li>$fdata['gender'] = $_POST['gender']; </li></ul><ul><li>$fdata['heard_from'] = $_POST['heard_from']; </li></ul><ul><li>$fdata['newsletter'] = $_POST['newsletter']; </li></ul><ul><li>print &quot;<h3>Data Got from the previous form:</h3>&quot;; </li></ul><ul><li>foreach($fdata as $key => $value){ </li></ul><ul><li>print &quot;<br>&quot;.$key.&quot; - Has ---------------> &quot;.$value; </li></ul><ul><li>} </li></ul>
  14. 14. MYSQL <ul><li>MYSQL is a free and open source relational database management system. </li></ul><ul><li>MYSQL has more than 11 million installations. </li></ul><ul><li>MYSQL runs as a server providing multi-user access to a number of databases. </li></ul><ul><li>It is a cross platform database server. </li></ul><ul><li>MySQL 5.x has many added features. </li></ul>
  15. 15. MYSQL Features <ul><li>Multiple storage engines (MyISAM, InnoDB…) </li></ul><ul><li>Views creation and update </li></ul><ul><li>Transactions with the InnoDB Engine </li></ul><ul><li>Sub Queries / Nested Select </li></ul><ul><li>Primary key and indexing </li></ul>
  16. 16. MYSQL data types/field types <ul><li>char( length ) – fixed length </li></ul><ul><li>varchar( 0-255 ) - variable length, occupies space as per the length of data. </li></ul><ul><li>Int( ) – signed and unsigned values, unsigned holds values from 0 to 4294967295. </li></ul><ul><li>Text – holds data character up to 65536 characters. </li></ul>
  17. 17. MYSQL data types/field types <ul><li>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 </li></ul><ul><li>Datetime – for time stamps format YYYY:MM:DD HH:MM:SS (date and time also possible separately) </li></ul><ul><li>ENUM(‘Option1’, ‘Option2’, … ‘Option n’) – for per specified fixed options like eye color can be black, brown, hazel, green only. </li></ul>
  18. 18. Tools to assist MYSQL development <ul><li>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 . </li></ul><ul><li>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. </li></ul>
  19. 19. DBDesigner 4
  20. 20. PHPMyAdmin
  21. 21. Questions???
  22. 22. Assignment <ul><li>Write a string taken input from a form to a file called user_input.txt and show it after reading from the same file. </li></ul><ul><li>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). </li></ul>
  23. 23. Lets start some Db design <ul><li>Using DB Designer 4 lets sketch the database for a login system. </li></ul><ul><li>Some MYSQL user management. </li></ul><ul><li>Then insert some users with use of PHPMyAdmin. </li></ul>

×