Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. <?php
  2.  
  3. define('ROOTPATH', __DIR__);
  4. $output = [];
  5. $output['result'] = [];
  6. $output['image_path'] = [];
  7. $applicationName = (isset($_POST) && array_key_exists('applicationName', $_POST)) ? $_POST['applicationName'] : 'applicationName';
  8.  
  9. if (empty($applicationName)) {
  10. $output['result'][] = 'missing application name';
  11. }
  12. else if (is_array($_FILES) && array_key_exists('image', $_FILES) && array_key_exists('logo', $_FILES))
  13. {
  14.  
  15. $upload_dir = '/upload_dir/';
  16. $upload_path = ROOTPATH . $upload_dir;
  17.  
  18. $applicationName = $_POST['applicationName'];
  19.  
  20. $sql_field_list = ['applicationName'];
  21. $sql_value_list = [$applicationName];
  22.  
  23. foreach ( $_FILES as $key => $upload) {
  24.  
  25. if($key != 'image' && $key != 'logo')
  26. {
  27. $output['result'][] = $key . ' is invalid image';
  28. }
  29. else
  30. {
  31.  
  32. if ($upload['error'] == UPLOAD_ERR_OK &&
  33. preg_match('#^image/(png|jpg|jpeg|gif)$#', strtolower($upload['type'])) && //ensure mime-type is image
  34. preg_match('#.(png|jpg|jpeg|gif)$#', strtolower($upload['name'])) ) //ensure name ends in trusted extension
  35. {
  36. $parts = explode('/', $upload['tmp_name']);
  37. $tmpName = array_pop($parts);
  38. $fieldname = ($key == 'image') ? 'bgBNPage' : 'logo';
  39. $filename = $applicationName . '_' . $fieldname . '.' . pathinfo($upload["name"], PATHINFO_EXTENSION);
  40.  
  41. if (move_uploaded_file($upload["tmp_name"], $upload_path . $filename))
  42. {
  43.  
  44. $sql_field_list[] = $fieldname;
  45. $sql_value_list[] = $upload_dir . $filename;
  46.  
  47. $output['image_path'][$key] = $upload_dir . $filename;
  48. }
  49. else
  50. {
  51. $output['result'][] = $key . ' upload fail';
  52. }
  53.  
  54. }
  55. else
  56. {
  57. $output['result'][] = $key . ' error while upload';
  58. }
  59. }
  60.  
  61. }
  62.  
  63. //after upload complete insert pic data into database
  64.  
  65. $con = mysqli_connect("localhost", "root", "root", "museum");
  66.  
  67. if (!$con) {
  68. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  69. }
  70.  
  71. $fields = implode(', ', $sql_field_list);
  72. $values = implode("', '", $sql_value_list);
  73. $sql = "REPLACE INTO general (" . $fields . ") VALUES ('" . $values . "');";
  74.  
  75. if (!mysqli_query($con, $sql)) {
  76. die('Error: ' . mysqli_error($con));
  77. }
  78.  
  79. mysqli_close($con);
  80.  
  81.  
  82. } else {
  83.  
  84. $output['result'][] = 'no file selected';
  85. }
  86.  
  87. header('Content-type: application/json');
  88. echo json_encode($output);
  89. echo json_encode('finish');
  90.  
  91. ?>
  92.  
  93. insert into general ([fields]) values ([values])
  94. on duplicate username update
  95. [whatever]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement