Advertisement
ivan_yosifov

Untitled

Dec 27th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>Demo</title>
  6.     <link href="style.css" rel="stylesheet" />
  7. </head>
  8. <body>
  9.     <h2>Demo upload</h2>
  10.    
  11. <?php
  12.     define('DB_HOST', 'localhost');
  13.     define('DB_USER', 'root');
  14.     define('DB_PASSWORD', 'pass');
  15.     define('DB_NAME', 'mydb');
  16.    
  17.     define('UPLOADPATH', 'images/');
  18.     define('MAXFILESIZE', 32768);
  19.    
  20.     if(isset($_POST['submit'])){
  21.         $name = trim($_POST['name']);
  22.         $score = trim($_POST['score']);
  23.         $screenshot = $_FILES['screenshot']['name'];
  24.         $screenshot_type = $_FILES['screenshot']['type'];
  25.         $screenshot_size = $_FILES['screenshot']['size'];
  26.            
  27.         echo $name.'<br>';
  28.         echo $score.'<br>';
  29.         echo $screenshot_type.'<br>';
  30.        
  31.         if(!empty($name) && !empty($score) && !empty($screenshot)){
  32.             if(($screenshot_type == 'image/gif' || $screenshot_type == 'image/jpeg'
  33.                 || $screenshot_type == 'image/pjpeg' || $screenshot_type == 'image/png')
  34.                     && ($screenshot_size > 0 && $screenshot_size <= MAXFILESIZE)){
  35.                 if($_FILES['screenshot']['error'] == 0){
  36.                     $target = UPLOADPATH.$screenshot;
  37.                     // if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)){
  38.                         $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
  39.                             or die('Error connecting to MySQL Server.');
  40.                        
  41.                         $query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score',
  42.                             '$screenshot')";
  43.                         mysqli_query($dbc, $query);
  44.                        
  45.                         echo '<p>Thanks for adding your new high score!</p>';
  46.                         echo '<p><strong>Name:</strong> '.$name.'<br>';
  47.                         echo '<strong>Score:</strong> '.$score.'<br>';
  48.                         echo '<img src="'.UPLOADPATH.$screenshot.'" alt="Score image" /></p>';
  49.                         echo '<p><a href="index.php">&lt;&lt; Back to high scores</a></p>';
  50.                        
  51.                         $name = "";
  52.                         $score = "";
  53.                        
  54.                         mysqli_close($dbc);
  55.                     //} else {
  56.                     //  echo '<p class="error">Sorry, there was a problem uploading your screen shot image.</p>';
  57.                     //}
  58.                 } else echo 'Error';
  59.             } else {
  60.                 echo '<p class="error">The screen shot must be a GIF, JPEG, or PNG image file no '.
  61.                     'greater than '.(MAXFILESIZE / 1024).' KB in size.</p>';
  62.             }
  63.            
  64.             //@unlink($_FILES['screenshot']['tmp_name']);
  65.         }else{
  66.             echo '<p class="error">Please enter all of the information to add your
  67.                 high score.</p>';
  68.         }
  69.     }
  70. ?>
  71.  
  72.     <hr />
  73.  
  74.     <form name="upload" enctype="multipart/form-data" method="post"
  75.         action="<?php echo $_SERVER['PHP_SELF']; ?>">
  76.         <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAXFILESIZE; ?>" />
  77.         <label for="name">Name:</label>
  78.         <input type="text" id="name" name="name"
  79.             value="<?php if(!empty($name)) echo $name; ?>" /><br>
  80.        
  81.         <label for="score">Score:</label>
  82.         <input type="text" id="score" name="score"
  83.             value="<?php if(!empty($score)) echo $score;  ?>" /><br>
  84.        
  85.        
  86.         <label for="screenshot">Screen shot:</label>
  87.  
  88.         <input type="file" id="screenshot" name="screenshot" />
  89.        
  90.         <hr />
  91.         <input type="submit" name="submit" value="Add" />
  92.     </form>
  93. </body>
  94. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement