Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ** database info:
- -----------------
- -----------------
- user = root;
- pass = ;
- db_name = fileupload;
- table_name = fileinfo;
- no_of_columns = 2;
- id = int auto increment;
- file = file varchar 255
- --------------------------------------
- ---------------------------------------
- config.php
- ----------
- ----------
- <?php
- define('DB_HOST', 'localhost');
- define('DB_USER', 'root');
- define('DB_PASS', '');
- define('DB_NAME', 'fileupload');
- ?>
- db.php
- --------
- --------
- <?php
- /**
- * database
- */
- class Database
- {
- public $host = DB_HOST;
- public $user = DB_USER;
- public $pass = DB_PASS;
- public $db = DB_NAME;
- public $error;
- public $link;
- public function __construct()
- {
- $this->ConectDb();
- }
- public function ConectDb()
- {
- $this->link = mysqli_connect( $this->host, $this->user, $this->pass, $this->db );
- if ( !$this->link ) {
- echo $this->error = "connection error".__LINE__;
- }
- }
- public function InsertFile($data)
- {
- if ( $this->link ) {
- mysqli_query( $this->link, $data);
- echo "data is inserted";
- }else {
- echo "inserting problem";
- }
- }
- }
- ?>
- index.php
- ----------
- ----------
- <?php
- include 'config.php';
- include 'db.php';
- $db = new Database();
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>file upload in oop</title>
- <style media="screen">
- *{
- margin: 0;padding: 0;outline: 0;
- }
- #main{
- margin: 20px auto;
- border: 1px solid black;
- border-radius: 10px;
- width: 500px;
- padding: 10px;
- }
- </style>
- </head>
- <body>
- <div id="main">
- <?php
- if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
- $accepted_ext = ['jpg', 'jpeg', 'png', 'gif'];
- $mainFile = $_FILES['filee']['name'];
- $tempFile = $_FILES['filee']['tmp_name'];
- $fileSize = $_FILES['filee']['size'];
- $folder = "image/";
- $file_name_seperation = explode( '.', $mainFile ); //to chunk a file after every dot
- $file_ext = end( $file_name_seperation ); //to hold the string after last dot
- $file_ext_lower = strtolower( $file_ext ); //make the extension lowercase
- $file_unique_name = substr( md5( time() ), 0, 10).".".$file_ext_lower; //hash string generate
- /* some condition for file */
- if ( !empty( $mainFile ) && $fileSize <= 5242880 && in_array( $file_ext_lower, $accepted_ext, TRUE ) ) {
- echo "file is accepted<br>";
- move_uploaded_file( $tempFile, $folder.$file_unique_name );
- $query = "insert into fileinfo(file) values('$file_unique_name')";
- $db->InsertFile($query);
- } else {
- if ( in_array( $file_ext_lower , $accepted_ext ) === FALSE || $fileSize > 5242880 || empty( $mainFile ) ) {
- echo "file isn't accepted";
- }
- }
- /* some condition for file */
- }
- ?>
- <form class="" action="" method="post" enctype="multipart/form-data">
- <table>
- <tr>
- <td>upload file:</td>
- <td><input type="file" name="filee" value=""></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" name="sub" value="upload"></td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement