greyhat49

index

Nov 22nd, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2. // Check for post data.
  3. if ($_POST && !empty($_FILES)) {
  4.     $formOk = true;
  5.    
  6.     $conn = mysqli_connect('localhost', 'root', '', 'test');
  7.  
  8.     //Assign Variables
  9.     $path = $_FILES['image']['tmp_name'];
  10.     $name = $_FILES['image']['name'];
  11.     $size = $_FILES['image']['size'];
  12.     $type = $_FILES['image']['type'];
  13.    
  14.    
  15.    
  16.         if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
  17.             $formOk = false;
  18.             echo "Error: Error in uploading file. Please try again.";
  19.         }
  20.  
  21.         //check file extension
  22.         if ($formOk && !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/gif'))) {
  23.             $formOk = false;
  24.             echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
  25.         }
  26.         // check for file size.
  27.         if ($formOk && filesize($path) > 500000) {
  28.             $formOk = false;
  29.             echo "Error: File size must be less than 500 KB.";
  30.         }
  31.  
  32.         if ($formOk) {
  33.             // read file contents
  34.             $content = file_get_contents($path);
  35.  
  36.             //connect to mysql database
  37.             if ($conn = mysqli_connect('localhost', 'root', '', 'test')) {
  38.                 //move uploaded file
  39.                 $new_file_name = uniqid() . "-" . $name;
  40.                 $file_path = 'upload/' . $new_file_name; //images haves to be a directory in the same directory that this script runs
  41.                 $uploaded_file = move_uploaded_file($_FILES['image']['tmp_name'],  $file_path );
  42.                
  43.                 if($uploaded_file == true){
  44.                     $content = mysqli_real_escape_string($conn, $content);
  45.                     $sql = "insert into images (name, size, type, content, path) values ('{$name}', '{$size}', '{$type}', '{$content}','{$file_path}')";
  46.  
  47.                     if (mysqli_query($conn, $sql)) {
  48.                         $uploadOk = true;
  49.                         $imageId = mysqli_insert_id($conn);
  50.                     } else {
  51.                         echo "Error: Could not save the data to mysql database. Please try again.";
  52.                     }
  53.                 }else{
  54.                     echo "Error: File can't to move into new directory.'";
  55.                 }
  56.                 mysqli_close($conn);
  57.             } else {
  58.                 echo "Error: Could not connect to mysql database. Please try again.";
  59.             }
  60.         }
  61. }
  62. ?>
  63.  
  64. <html>
  65.     <head>
  66.         <title>Upload image to mysql database.</title>
  67.         <style type="text/css">
  68.             img{
  69.                 margin: .2em;
  70.                 border: 1px solid #555;
  71.                 padding: .2em;
  72.                 vertical-align: top;
  73.             }
  74.         </style>
  75.     </head>
  76.     <body>
  77.         <?php if (!empty($uploadOk)): ?>
  78.             <div>
  79.                 <h3>Image Uploaded:</h3>
  80.             </div>
  81.             <div>
  82.                 <img src="<?php echo $file_path ?>" width="150px">
  83.                 <strong>Embed</strong>: <input size="25" value='<img src="image.php?id=<?=$imageId ?>">'>
  84.             </div>
  85.  
  86.             <hr>
  87.         <?php endif; ?>
  88.  
  89.         <form action="<?php $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" >
  90.           <div>
  91.             <h3>Image Upload:</h3>
  92.           </div>
  93.           <div>
  94.             <label>Image</label>
  95.             <input type="hidden" name="MAX_FILE_SIZE" value="500000">
  96.             <input type="file" name="image" />
  97.             <input name="submit" type="submit" value="Upload">
  98.           </div>
  99.         </form>
  100.     </body>
  101. </html>
Advertisement
Add Comment
Please, Sign In to add comment