farfnd

create_daftar.php

Dec 14th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. <?php
  2.  
  3. include("config.php");
  4.  
  5. if($_SERVER['REQUEST_METHOD'] == 'POST'){
  6.     $nama = str_replace("'", "\'", $_POST['nama']);
  7.     $jenis_kelamin = str_replace("'", "\'", $_POST['jenis_kelamin']);
  8.     $agama = str_replace("'", "\'", $_POST['agama']);
  9.     $sekolah_asal = str_replace("'", "\'", $_POST['sekolah_asal']);
  10.     $alamat = str_replace("'", "\'", $_POST['alamat']);
  11.     $foto = "";
  12.  
  13.     if(isset($_FILES['foto']['name'])){
  14.  
  15.         if($_FILES['foto']['size'] > 3*1048576) { //3 MB (size is also in bytes)
  16.             die(json_encode([
  17.                 "error" => 500,
  18.                 "status" => "File is too large (> 3 MB)"
  19.             ]));
  20.             exit;
  21.         }
  22.  
  23.         $filename = $_FILES['foto']['name'];
  24.        
  25.         $location = "../images/".$filename;
  26.         $imageFileType = pathinfo($location,PATHINFO_EXTENSION);
  27.         $imageFileType = strtolower($imageFileType);
  28.         $imageNewFileName = md5(time()).'.'.$imageFileType;
  29.         $location = "../images/".$imageNewFileName;
  30.  
  31.         $valid_extensions = array("jpg","jpeg","png");
  32.      
  33.         $response = 0;
  34.         /* Check file extension */
  35.         if(in_array(strtolower($imageFileType), $valid_extensions)) {
  36.            /* Upload file */
  37.            if(move_uploaded_file($_FILES['foto']['tmp_name'], $location)){
  38.               $response = $location;
  39.               $foto = $imageNewFileName;
  40.            }
  41.         }else{
  42.             die(json_encode([
  43.                 "error" => 500,
  44.                 "status" => "Invalid file type"
  45.             ]));
  46.             exit;
  47.         }    
  48.     }
  49.  
  50.  
  51.     $sql = "INSERT INTO calon_siswa (nama, jenis_kelamin, agama, sekolah_asal, alamat, foto)
  52.            VALUE ('$nama', '$jenis_kelamin', '$agama', '$sekolah_asal', '$alamat', '$foto')";
  53.     $query = mysqli_query($db, $sql);
  54.  
  55.     if ($query) {
  56.         die(json_encode([
  57.             "error" => 0,
  58.             "status" => "OK"
  59.         ]));
  60.     } else {
  61.         die(json_encode([
  62.             "error" => 500,
  63.             "status" => "Internal Server Error"
  64.         ]));
  65.     }
  66. }else{
  67.     die("Method not allowed");
  68. }
Advertisement
Add Comment
Please, Sign In to add comment