Advertisement
Guest User

Untitled

a guest
Sep 15th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. <?php // upload.php
  2. // 'images' refers to your file input name attribute
  3. if (empty($_FILES['images'])) {
  4.     echo json_encode(['success'=>'No files found for upload.']);
  5.     // or you can throw an exception
  6.     return; // terminate
  7. }
  8.  
  9. // get the files posted
  10. $images = $_FILES['images'];
  11.  
  12.  
  13.  
  14. // a flag to see if everything is ok
  15. $success = null;
  16.  
  17. // file paths to store
  18. $paths= [];
  19.  
  20. // get file names
  21. $filenames = $images['name'];
  22.  
  23.  
  24. // loop and process files
  25. for($i=0; $i < count($filenames); $i++){
  26.     $ext = explode('.', basename($filenames[$i]));
  27.     $target = "images" . DIRECTORY_SEPARATOR . md5(uniqid()) . "." . array_pop($ext);
  28.     if(move_uploaded_file($images['tmp_name'][$i], $target)) {
  29.         $success = true;
  30.         $paths[] = $target;
  31.     } else {
  32.         $success = false;
  33.         break;
  34.     }
  35. }
  36.  
  37. // check and process based on successful status
  38. if ($success === true) {
  39.     // call the function to save all data to database
  40.     // code for the following function `save_data` is not
  41.     // mentioned in this example
  42. $servername = "localhost";
  43. $username = "root";
  44. $password = "";
  45. $dbname = "keviandra";
  46.  
  47. try {
  48.     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  49.     // set the PDO error mode to exception
  50.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  51.     $sql = "INSERT INTO `gambar` (`image_id`, `url`) VALUES (NULL, '$target')";
  52.     // use exec() because no results are returned
  53.     $conn->exec($sql);
  54.     echo $target;
  55.     echo "New record created successfully";
  56.     }
  57. catch(PDOException $e)
  58.     {
  59.     echo $sql . "<br>" . $e->getMessage();
  60.     }
  61.  
  62. $conn = null;
  63.  
  64.     // store a successful response (default at least an empty array). You
  65.     // could return any additional response info you need to the plugin for
  66.     // advanced implementations.
  67.     $output = [];
  68.     // for example you can get the list of files uploaded this way
  69.     // $output = ['uploaded' => $paths];
  70. } elseif ($success === false) {
  71.     $output = ['error'=>'Error while uploading images. Contact the system administrator'];
  72.     // delete any uploaded files
  73.     foreach ($paths as $file) {
  74.         unlink($file);
  75.     }
  76. } else {
  77.     $output = ['error'=>'No files were processed.'];
  78. }
  79.  
  80. // return a json encoded response for plugin to process successfully
  81. echo json_encode($output);
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement