SHARE
TWEET

no.94_file_upload_validation_oop_image

Riju18 Apr 27th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ** database info:
  2. -----------------
  3. -----------------
  4. user = root;
  5. pass = ;
  6. db_name = fileupload;
  7. table_name = fileinfo;
  8. no_of_columns = 2;
  9.         id = int auto increment;
  10.         file = file varchar 255
  11.  
  12. --------------------------------------
  13. ---------------------------------------
  14.  
  15.  
  16. config.php
  17. ----------
  18. ----------
  19. <?php
  20.   define('DB_HOST', 'localhost');
  21.   define('DB_USER', 'root');
  22.   define('DB_PASS', '');
  23.   define('DB_NAME', 'fileupload');
  24.  ?>
  25.  
  26. db.php
  27. --------
  28. --------
  29. <?php
  30.   /**
  31.    * database
  32.    */
  33.   class Database
  34.   {
  35.  
  36.     public $host = DB_HOST;
  37.     public $user = DB_USER;
  38.     public $pass = DB_PASS;
  39.     public $db = DB_NAME;
  40.  
  41.     public $error;
  42.     public $link;
  43.  
  44.     public function __construct()
  45.     {
  46.       $this->ConectDb();
  47.     }
  48.  
  49.     public function ConectDb()
  50.     {
  51.       $this->link = mysqli_connect( $this->host, $this->user, $this->pass, $this->db );
  52.       if ( !$this->link ) {
  53.         echo $this->error = "connection error".__LINE__;
  54.       }
  55.     }
  56.  
  57.     public function InsertFile($data)
  58.     {
  59.       if ( $this->link ) {
  60.         mysqli_query( $this->link, $data);
  61.         echo "data is inserted";
  62.       }else {
  63.         echo "inserting problem";
  64.       }
  65.     }
  66.   }
  67.  
  68.  ?>
  69.  
  70. index.php
  71. ----------
  72. ----------
  73.  
  74. <?php
  75.     include 'config.php';
  76.     include 'db.php';
  77.     $db = new Database();
  78.  ?>
  79.  
  80. <!DOCTYPE html>
  81. <html lang="en">
  82. <head>
  83.   <meta charset="UTF-8">
  84.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  85.   <meta http-equiv="X-UA-Compatible" content="ie=edge">
  86.   <title>file upload in oop</title>
  87.   <style media="screen">
  88.     *{
  89.       margin: 0;padding: 0;outline: 0;
  90.     }
  91.     #main{
  92.       margin: 20px auto;
  93.       border: 1px solid black;
  94.       border-radius: 10px;
  95.       width: 500px;
  96.       padding: 10px;
  97.     }
  98.   </style>
  99. </head>
  100. <body>
  101.   <div id="main">
  102.     <?php
  103.       if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
  104.  
  105.           $accepted_ext = ['jpg', 'jpeg', 'png', 'gif'];
  106.  
  107.           $mainFile = $_FILES['filee']['name'];
  108.           $tempFile = $_FILES['filee']['tmp_name'];
  109.           $fileSize = $_FILES['filee']['size'];
  110.           $folder = "image/";
  111.  
  112.           $file_name_seperation = explode( '.', $mainFile ); //to chunk a file after every dot
  113.           $file_ext = end( $file_name_seperation );   //to hold the string after last dot
  114.           $file_ext_lower = strtolower( $file_ext );  //make the extension lowercase
  115.           $file_unique_name = substr( md5( time() ), 0, 10).".".$file_ext_lower; //hash string generate
  116.  
  117.           /*  some condition for file  */
  118.           if (   !empty( $mainFile ) && $fileSize <= 5242880 && in_array( $file_ext_lower, $accepted_ext, TRUE ) ) {
  119.             echo "file is accepted<br>";
  120.  
  121.             move_uploaded_file( $tempFile, $folder.$file_unique_name );
  122.             $query = "insert into fileinfo(file) values('$file_unique_name')";
  123.             $db->InsertFile($query);
  124.           } else {
  125.             if ( in_array( $file_ext_lower , $accepted_ext ) === FALSE || $fileSize > 5242880 || empty( $mainFile ) ) {
  126.               echo "file isn't accepted";
  127.             }
  128.           }
  129.  
  130.           /*  some condition for file  */
  131.       }
  132.      ?>
  133.     <form class="" action="" method="post" enctype="multipart/form-data">
  134.       <table>
  135.         <tr>
  136.           <td>upload file:</td>
  137.           <td><input type="file" name="filee" value=""></td>
  138.         </tr>
  139.         <tr>
  140.           <td></td>
  141.           <td><input type="submit" name="sub" value="upload"></td>
  142.         </tr>
  143.       </table>
  144.     </form>
  145.   </div>
  146. </body>
  147. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top