Advertisement
SomniP

Маршрут Загрузка изображения на сервер

Dec 4th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2. //require_once __DIR__ . '/vendor/autoload.php';
  3. //require_once __DIR__.'\vendor\autoload.php';
  4.  
  5. use Slim\Http\Request;
  6. use Slim\Http\Response;
  7. use Slim\Http\UploadedFile;
  8.  
  9. function Check_source($th)
  10. {
  11.     if (isset($_COOKIE['idUser']) and isset($_COOKIE['Access_key']))
  12.     {
  13.         $idU = $_COOKIE['idUser'];
  14.         $ak_cookie = $_COOKIE['Access_key'];
  15.         $sth = $th->db->prepare(
  16.             "
  17.        SELECT Rank, Access_key
  18.        FROM Users
  19.        WHERE idUsers = :idUser
  20.        "
  21.         );
  22.         $sth->bindParam("idUser", $idU);
  23.         $sth->execute();
  24.  
  25.         $info_ = $sth->fetchAll();
  26.         $ak_bd = $info_[0]['Access_key'];
  27.         $rank = $info_[0]['Rank'];
  28.  
  29.         if($ak_bd == $ak_cookie)
  30.         {
  31.             return $rank;
  32.         }
  33.         else
  34.         {
  35.             return -1;
  36.         }
  37.     }
  38.     else
  39.         return -1;
  40.  
  41.  
  42. }
  43.  
  44. function Read_id_user_from_cookie()
  45. {
  46.     if (isset($_COOKIE['idUser']))
  47.     {
  48.         return $_COOKIE['idUser'];
  49.     }
  50.     else
  51.     {
  52.         return $this->response->withRedirect('/auth');
  53.     }
  54. }
  55.  
  56.  
  57. function moveUploadedFile($directory, UploadedFile $uploadedFile)
  58. {
  59.     $extension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
  60.     $basename = bin2hex(random_bytes(8)); // see http://php.net/manual/en/function.random-bytes.php
  61.     $filename = sprintf('%s.%0.8s', $basename, $extension);
  62.  
  63.     $uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);
  64.  
  65.     return $filename;
  66. }
  67.  
  68. //Загрузка нового или изменение изображения аватарки пользователя
  69. $app->post('/Set_ava', function(Request $request, Response $response, $args)
  70. {
  71.     $set_rank = Check_source($this);
  72.     if($set_rank != -1)
  73.     {
  74.         $idU = Read_id_user_from_cookie();
  75.         //Каталог на сервере куда уходит файл
  76.         $directory = "../../ava";
  77.         //echo getcwd();
  78.         //Загружаемый файл
  79.         $uploadedFiles = $request->getUploadedFiles();
  80.         $uploadedFile = $uploadedFiles['img'];
  81.  
  82.         //Запрос к БД
  83.         $sth = $this->db->prepare(
  84.             "
  85.         SELECT IFNULL(Url,-1) name_file
  86.         FROM Users
  87.         WHERE idUsers = :idUser
  88.            "
  89.         );
  90.         $sth->bindParam("idUser", $idU);
  91.         $sth->execute();
  92.         $nf = $sth->fetchAll()[0]['name_file'];
  93.  
  94.         if($nf != -1)
  95.         {
  96.             //Запрос к БД
  97.             $sth2 = $this->db->prepare(
  98.                 "
  99. SELECT Url
  100.         FROM Users
  101.         WHERE idUsers = :idU
  102. "
  103.             );
  104.             $sth2->bindParam("idU", $idU);
  105.             $sth2->execute();
  106.             $name_pic = $sth2->fetchAll()[0]['Url'];
  107.             $filename = $directory.DIRECTORY_SEPARATOR.$name_pic;
  108.             if ( !(@unlink($filename)) ) die('Error Delete File.');
  109.         }
  110.  
  111.     if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
  112.  
  113.         $filename2 = moveUploadedFile($directory, $uploadedFile);
  114.         $response->write('uploaded ' . $filename2 . '<br/>');
  115.  
  116.         //Запрос к БД
  117.         $sth3 = $this->db->prepare(
  118.             "
  119. UPDATE Users
  120. SET Url = :nm
  121. WHERE idUsers = :idU
  122. "
  123.         );
  124.         $sth3->bindParam("nm", $filename2);
  125.         $sth3->bindParam("idU", $idU);
  126.         $sth3->execute();
  127.     }
  128.  
  129.         return $this->response->withJson(0);
  130.     }
  131.     else
  132.     {
  133.         return $this->response->withRedirect('/auth');
  134.     }
  135. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement