Advertisement
Guest User

Untitled

a guest
May 24th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.42 KB | None | 0 0
  1. class Gallery {
  2.  
  3. public function upload_pictures($images, $project_id_, $author_) {
  4.   global $db, $msg;
  5.  
  6. if(!empty($images['name'])) {
  7.  
  8.   //$images = $_FILES['images'];
  9.   $uploaded = array();
  10.   $failed = array();
  11.   $allowed = array('jpg', 'jpeg', 'gif', 'png');
  12.   //var_dump($images);
  13.   foreach($images['name'] as $position => $image_name) {
  14.     $image_tmp = $images['tmp_name'] [$position];
  15.     $image_name = $images['name'] [$position];
  16.     $image_size = $images['size'] [$position];
  17.     $image_error = $images['error'] [$position];
  18.  
  19.     $image_ext = explode('.', $image_name);
  20.     $image_ext = strtolower(end($image_ext));
  21.  
  22.     if(in_array($image_ext, $allowed)) {
  23.       if($image_error === 0) {
  24.         if($image_size <=30000000) {
  25.           $image_name_new = uniqid().'_'.$image_name;
  26.           $image_destination = APP_ROOT. '//uploads//' .$image_name_new;
  27.           if(move_uploaded_file($image_tmp, $image_destination)) {
  28.             $uploaded[$position] = $image_name_new;
  29.             $this->insert_picture_db($project_id_, $image_name_new, $author_);
  30.           } else {$failed[$position] = '['.$image_name.']';}
  31.  
  32.         } else {$failed[$position] = '['.$image_name.'] fails ir par lielu.';}
  33.     } else {$failed[$position] = '['.$image_name.']'. ' bildes formāts nav atļauts.'; }
  34.   }
  35. if(!empty($uploaded)) {
  36.   foreach($uploaded as $key => $val) {
  37.     echo $val;
  38.   }
  39. }
  40.  
  41. if(!empty($failed)) {
  42.   //print_r($failed);
  43. }
  44. }
  45. }
  46. }
  47.  
  48. public function insert_picture_db($project_id, $picture, $author) {
  49.   global $db;
  50.  
  51.   $sql_pict = $db->prepare('INSERT INTO PICTURES (`project_id`, `picture`, `author`) VALUES (:project_id, :picture, :author)');
  52.   $sql_pict2 = $sql_pict->execute([
  53.     'project_id' => $project_id,
  54.     'picture' => $picture,
  55.     'author' => $author,
  56.   ]);
  57. var_dump($sql_pict);
  58.  
  59. }
  60.  
  61. public function show_edit_images($project_id_img) {
  62.   global $db;
  63.  
  64.  
  65.  
  66.   $sh_ed_img1 = $db->prepare('SELECT `picture`, `id` FROM `pictures` WHERE `project_id` = :project_id_img');
  67.   $sh_ed_img1->execute([
  68.     'project_id_img'=>$project_id_img,
  69.   ]);
  70.   $sh_ed_img2 = $sh_ed_img1->fetchAll();
  71.   if($sh_ed_img2){
  72.  
  73.     echo '<div class="container padding-zero" style="padding-bottom: 10px; margin-left: -6px;">
  74.      <div class="col-xs-12 padding-zero">';
  75.  
  76.   foreach ($sh_ed_img2 as $picture) {
  77.     //var_dump($picture['picture']);
  78.     echo '<div class="col-xs-3" style="height: 140px; width: 140px; background: url(\''. BASE_URL. '//uploads//' . $picture['picture'].'\'); background-size:cover; margin: 5px;"><a href="edit_project.php?id='.$project_id_img.'&del_img='.$picture['id'].'"><img src="img/icons/delete.png" style="top: 4px;left: 100px;position: relative;height: 20px;width: 20px;"></a></div>';
  79.   }
  80.  
  81.     echo '
  82.    </div>
  83.    </div>';
  84. }
  85.  
  86. }
  87.  
  88. public function delete_image($del_img_id) {
  89.  
  90.   global $db, $user_id_session;
  91.   $author_of_del = Misc::get_field_with_where('author', 'pictures', 'id', $del_img_id);
  92.   //var_dump($img_name_of_del);
  93.   $img_name_of_del = Misc::get_field_with_where('picture', 'pictures', 'id', $del_img_id);
  94.     var_dump($user_id_session);
  95.   if (($user_id_session === $author_of_del) || ($user_id_session === User::is_online_admin())) {
  96.  
  97.   $del_img_sql = $db->prepare('DELETE FROM `pictures` WHERE `id` = :del_img_id');
  98.   $del_img_sql->execute([
  99.     'del_img_id'=>$del_img_id,
  100.   ]);
  101.   $del_file = APP_ROOT. '//uploads//' . $img_name_of_del;
  102.   unlink($del_file);
  103. }
  104. }
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement