ACC SHELL

Path : /srv/www/vhosts/agroing/web/models/
File Upload :
Current File : /srv/www/vhosts/agroing/web/models/files_model.php

<?php

/**
 * Def
 *
 * @author Melounek
 */
/*
 * TODO
 */
class Files_model extends Base_model {

	public $table = "files";
	public static $files_folder;

	public function __construct() {
		$this->set_useful_columns(array("id_sites","file_url","file_title","file"));
	}

	// get_all() ... extends Base_model
	// get_one() ... extends Base_model

	/**
	 * @param array $data important index is uploaded_file!
	 * @param int $id
	 */
	public function save_file($data=NULL, $id=NULL) {
		$this->load($data, $id);
		//if(empty($this->a['uploaded_file']['name'])) return false;
		if(empty($this->a['file']) && !empty($this->a['uploaded_file']['name'])){ // create file_name from name
			$e = explode(".",$this->a['uploaded_file']['name']);
			array_pop($e);
			$this->a['file'] = implode("",$e);
			Tools::debug($this->a['file']);
		}
		if (!$this->id) { // ukládat jen nové soubory
			$this->a['uploaded_file']['name'] = Tools::str2url($this->a['uploaded_file']['name']);
			if(!($this->a["file_url"] = $this->a['uploaded_file']['name'])) {
				Tools::critical("Files_model::save_file() empty url passed in data: ".var_export($data,1));
				return false;
			}
			$dest = self::$files_folder . $this->a['uploaded_file']['name']; 
			if (in_array($this->a['uploaded_file']['type'], array('image/jpeg', 'image/png', 'image/gif'))) {
				if(!$this->upload_img($dest)) return false; // resize big images
			} else {
				if(!move_uploaded_file($this->a['uploaded_file']['tmp_name'], $dest)) return false;
			}
		}
		$return = $this->save();
		if($return)	Tools::debug("file saved. \$data: " . var_export($data,1));
		return $return;
	}

	public function del_files($id=NULL) {
		$this->get_one($id);
		$pom = $this->db->query("SELECT COUNT(*) as pocet FROM files WHERE file_url = '" . $this->a["file_url"] . "';")->row_array();
		if(($ret = $this->del())){
			$file = self::$files_folder . $this->a["file_url"];
			if ($pom["pocet"] <= 1) {
				if(file_exists($file)){
					Tools::debug("Delete file id:$this->id (".$this->a["file_url"].") from database and directory");
					unlink($file);
				} else {
					Tools::critical("files_model::del_file() Try to delete file, which wasn't in directory - " . $file . " id:$this->id");
				}
			}else{
				Tools::debug("Delete file id:$this->id (".$this->a["file_url"].") from database. NO from directory");
			}
		}else{
			Tools::critical("Deleting unexsisting file id:$this->id");
		}
		return $ret;
	}
	// save img in max_size 1200x900
	private function upload_img($img_dest) {
		$img = $this->a['uploaded_file']['tmp_name'];
		if (!empty($img)) {
			$image = new Image();
			$image->load($img);
			$image->resize_max(1600, 1200, false);
			if($image->save($img_dest)){
				Tools::log("files_model::upload_img() saved img ".$img_dest);
				return true;
			}
		}
		Tools::critical("files_model::upload_img() error with saving name='$img_dest' tmp_name='$img'");
		return false;
	}

}

ACC SHELL 2018