Skip to content

Instantly share code, notes, and snippets.

@obojdi
Created August 1, 2017 16:35
Show Gist options
  • Save obojdi/80204aa7ba7850d9c5526456170ff8e5 to your computer and use it in GitHub Desktop.
Save obojdi/80204aa7ba7850d9c5526456170ff8e5 to your computer and use it in GitHub Desktop.
Image uploader class
<?php
/**
* Image uploader
*/
class ImageUploader
{
private $this;
private $id;
private $uploadPath;
public $updateId;
public $itemId;
public $url;
public $tempName;
public $newName;
public $addSql;
function __construct()
{
// $this = this;
}
/**
* === Блок сеттеров и геттеров ===
*/
/**
* установка времменного имени загруженного файла
* @param string $name
* @return void
*/
public function setTempName($name)
{
$this->tempName = $name;
}
/**
* возврат пути к временному файлу
* @return string
*/
public function getTempName()
{
return $this->tempName;
}
/**
* установка имени загруженного файла
* @param string $name
* @return void
*/
public function setName($name)
{
$this->newName = $name;
}
/**
* установка url
* @param string $url
* @return void
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* возврат строки url
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* установка url
* @param string $url
* @return void
*/
public function setItemId($id)
{
$this->itemId = $id;
}
/**
* установка пути к папке upload вне корня www
*/
public function getImageId($parent = 0, $type = "object", $sql = "")
{
$sql = "SELECT id FROM [db] WHERE [type]='{$type}' AND [parent]={$parent}";
$query = mssql_query($sql);
$id = mssql_result($query, 0, 'id');
// if this is false then use addSql()
$this->updateId = $id;
}
/**
* установка пути к папке upload вне корня www
*/
public function setUploadPath($path)
{
$this->uploadPath = $path;
}
/**
* подготовка запроса на добавление в базу
* @param number $parent (set to $this->parent)
* @param string $img
* @param string $type
* @param number $video
* @param number $position
* @param string $size
* @param string $sql
*/
public function addSql($parent = "", $img_name = "", $type = "", $video = "0", $position = "0", $size = "normal", $sql = "")
{
$sql = "INSERT INTO [db]
([parent], [img], [type], [video], [position], [size])
VALUES ($parent, '{$img_name}', '{$type}', {$video}, {$position}, '{$size}')";
$this->addSql = $sql;
// $this->addSql = mysql_real_escape_string($sql);
}
/**
* подготовка запроса на изменение в базу
*/
public function updateSql($parent = 0, $img_name = "", $type = "", $video = "0", $position = "0", $size = "normal", $sql = "")
{
$sql = "UPDATE [db] SET
[parent] = {$parent}
, [img] = '{$img_name}'
, [type] = '{$type}'
, [video] = {$video}
, [position] = {$position}
, [size] = '{$size}'
WHERE id = {$this->updateId}
";
$this->updateSql = $sql;
}
/**
* добавление в базу
*/
public function addQuery()
{
// print $this->addSql;
mssql_query($this->addSql);
}
/**
* обновление базы
*/
public function updateQuery()
{
// print $this->updateSql;
mssql_query($this->updateSql);
}
/**
* перемещение загруженного файла
*/
public function uploadFile()
{
// pr($this->uploadPath . "/" . $this->newName);
if(!is_dir($this->uploadPath))
{
mkdir($this->uploadPath, 0775, true);
}
move_uploaded_file($this->tempName, $this->uploadPath . "/" . $this->newName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment