Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //require_once __DIR__ . '/vendor/autoload.php';
- //require_once __DIR__.'\vendor\autoload.php';
- use Slim\Http\Request;
- use Slim\Http\Response;
- use Slim\Http\UploadedFile;
- function Check_source($th)
- {
- if (isset($_COOKIE['idUser']) and isset($_COOKIE['Access_key']))
- {
- $idU = $_COOKIE['idUser'];
- $ak_cookie = $_COOKIE['Access_key'];
- $sth = $th->db->prepare(
- "
- SELECT Rank, Access_key
- FROM Users
- WHERE idUsers = :idUser
- "
- );
- $sth->bindParam("idUser", $idU);
- $sth->execute();
- $info_ = $sth->fetchAll();
- $ak_bd = $info_[0]['Access_key'];
- $rank = $info_[0]['Rank'];
- if($ak_bd == $ak_cookie)
- {
- return $rank;
- }
- else
- {
- return -1;
- }
- }
- else
- return -1;
- }
- function Read_id_user_from_cookie()
- {
- if (isset($_COOKIE['idUser']))
- {
- return $_COOKIE['idUser'];
- }
- else
- {
- return $this->response->withRedirect('/auth');
- }
- }
- function moveUploadedFile($directory, UploadedFile $uploadedFile)
- {
- $extension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
- $basename = bin2hex(random_bytes(8)); // see http://php.net/manual/en/function.random-bytes.php
- $filename = sprintf('%s.%0.8s', $basename, $extension);
- $uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);
- return $filename;
- }
- //Загрузка нового или изменение изображения аватарки пользователя
- $app->post('/Set_ava', function(Request $request, Response $response, $args)
- {
- $set_rank = Check_source($this);
- if($set_rank != -1)
- {
- $idU = Read_id_user_from_cookie();
- //Каталог на сервере куда уходит файл
- $directory = "../../ava";
- //echo getcwd();
- //Загружаемый файл
- $uploadedFiles = $request->getUploadedFiles();
- $uploadedFile = $uploadedFiles['img'];
- //Запрос к БД
- $sth = $this->db->prepare(
- "
- SELECT IFNULL(Url,-1) name_file
- FROM Users
- WHERE idUsers = :idUser
- "
- );
- $sth->bindParam("idUser", $idU);
- $sth->execute();
- $nf = $sth->fetchAll()[0]['name_file'];
- if($nf != -1)
- {
- //Запрос к БД
- $sth2 = $this->db->prepare(
- "
- SELECT Url
- FROM Users
- WHERE idUsers = :idU
- "
- );
- $sth2->bindParam("idU", $idU);
- $sth2->execute();
- $name_pic = $sth2->fetchAll()[0]['Url'];
- $filename = $directory.DIRECTORY_SEPARATOR.$name_pic;
- if ( !(@unlink($filename)) ) die('Error Delete File.');
- }
- if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
- $filename2 = moveUploadedFile($directory, $uploadedFile);
- $response->write('uploaded ' . $filename2 . '<br/>');
- //Запрос к БД
- $sth3 = $this->db->prepare(
- "
- UPDATE Users
- SET Url = :nm
- WHERE idUsers = :idU
- "
- );
- $sth3->bindParam("nm", $filename2);
- $sth3->bindParam("idU", $idU);
- $sth3->execute();
- }
- return $this->response->withJson(0);
- }
- else
- {
- return $this->response->withRedirect('/auth');
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement