• Like
  • Save
05 File Handling Upload Mysql
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

05 File Handling Upload Mysql

  • 1,977 views
Published

File Handing in PHP basics

File Handing in PHP basics

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,977
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. PHP Day 05 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com http://www.php.net
  • 2. PHP File Handling Funcitons
    • Fopen(“pathoileile_name.ext”, “mode”);
    • Returns a resource.
    • $handle = fopen("c:atanfo.txt", "r");
  • 3. 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 );
  • 4. 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
  • 5. 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
  • 6. 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);
    • ?>
  • 7. 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>
  • 8. $_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.
  • 9. $_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
  • 10. 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>
  • 11. 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
  • 12. A Form code with all elements Form Code at day05prog46_test_form.php
  • 13. 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;
    • }
  • 14. 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.
  • 15. MYSQL Features
    • Multiple storage engines (MyISAM, InnoDB…)
    • Views creation and update
    • Transactions with the InnoDB Engine
    • Sub Queries / Nested Select
    • Primary key and indexing
  • 16. 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.
  • 17. 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.
  • 18. 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.
  • 19. DBDesigner 4
  • 20. PHPMyAdmin
  • 21. Questions???
  • 22. 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).
  • 23. 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.