public static function postImage($file, $filename, $uploadDir, $isThumb = false, $width = 800, $uploadThumb = "", $widthThumb = 150) { $destinationPath = $uploadDir; if (!file_exists($destinationPath)) { mkdir($destinationPath, 0777); } if ($uploadThumb != "") { if (!file_exists($uploadThumb)) { mkdir($uploadThumb, 0777); } } $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); $filename = $filename . "." . $extension; $uploadSuccess = ""; if ($extension == "jpg" || $extension == "jpeg" || $extension == "gif" || $extension == "png" || $extension == "tiff") { $uploadSuccess1 = move_uploaded_file($file['tmp_name'], $destinationPath . "/" . $filename); if (!$uploadSuccess1) { return; } if ($isThumb === true) { $uploadSuccess2 = copy($destinationPath . "/" . $filename, $uploadThumb . "/" . $filename); } } ImageResize::load($destinationPath . "/" . $filename); ImageResize::resizeToWidth($width); ImageResize::save(); if ($isThumb === true) { ImageResize::load($uploadThumb . "/" . $filename); ImageResize::resizeToWidth($widthThumb); ImageResize::save(); } return $filename; }
public function doUpload() { // removeFilesBeforeUpload if ($this->removeFilesBeforeUpload) { $this->removeFiles(); } if (!isset($_FILES[$this->campo]['name'])) { return false; } $this->file = $_FILES[$this->campo]['name']; $this->file_tmp = $_FILES[$this->campo]['tmp_name']; $this->file_type = $_FILES[$this->campo]['type']; $this->file_size = $_FILES[$this->campo]['size']; $this->cantidad = count($this->file); // ITERATE FILES for ($x = 0; $x <= $this->cantidad; $x++) { // IF NOT SET FILENAME, EXIT if (!isset($this->file[$x])) { return false; } // IF FILENAME EMPTY, EXIT if (empty($this->file[$x])) { return false; } // FORMATEO DE LA FOTO $f = $this->cleanUploadFileName($this->file[$x]); // CHECK 10 min chars of file name (1-ee-a.jpg) if (strlen($f) < 6) { if (DEBUGIN == true) { $msg = "filename " . $f . " < 6 min chars"; } die($msg); } // GET REAL EXTENSION $ext = $this->extension($this->file[$x]); // CHECK EXTENCION AND MIME TYPE if (!in_array($ext, $this->acceptedExtencions) || !in_array($this->file_type[$x], $this->acceptedFileTypes)) { if (DEBUGIN == true) { $msg = "not allowed filetype: " . $this->file_type[$x] . " or extencion: " . $ext; } die($msg); } // RESIZE IMG if ($this->resizeToWidth || $this->resizeToHeight) { $imageSize = getimagesize($this->file_tmp[$x]); $image = new ImageResize(); $image->load($this->file_tmp[$x]); if ($this->resizeToWidth && $imageSize[0] >= $this->resizeToWidth) { $image->resizeToWidth($this->resizeToWidth); } if ($this->resizeToHeight && $imageSize[1] >= $this->resizeToHeight) { $image->resizeToHeight($this->resizeToHeight); } $image->save($this->file_tmp[$x]); } // CHMOD chmod($this->file_tmp[$x], $this->fileCHMOD); // UPLOADED if (!is_uploaded_file($this->file_tmp[$x])) { if (DEBUGIN == true) { $msg = "file not uploaded"; } die($msg); } // MOVE ALT FILE if ($this->copyToAltPath) { // MAKE ALT DIR $this->mkdir_recursive($this->altPath); // COPY $sufix = $x; if (!copy($this->file_tmp[$x], $this->altPath . $this->altPathFileName . $sufix . '.jpg')) { if (DEBUGIN == true) { $msg = "alt file not copied"; } die($msg); } } // MAKE DIR $this->mkdir_recursive('uploads/' . $this->tabla . '/', $this->dirCHMOD); // MOVE FILE if (!move_uploaded_file($this->file_tmp[$x], 'uploads/' . $this->tabla . '/' . $f . '.' . $ext)) { if (DEBUGIN == true) { $msg = "file not moved"; } die($msg); } // INSERT FILES EN OTRA TABLA SI HAY ID Y SI SE LOGRO SUBIR if (isset($this->id) && $this->id != 0) { sq("INSERT INTO files SET item_id=" . $this->id . ", nombre='" . $f . "." . $ext . "', size='" . $this->file_size[$x] . "', tabla='" . $this->tabla . "', cat_id='" . $this->cat_id . "', subcat_id='" . $this->subcat_id . "', tipo='" . $this->campo . "', titulo='" . $this->titulo . "', type='" . $this->file_type[$x] . "', dateLastUpdate=NOW(), dateInsert=NOW() "); } else { if (DEBUGIN == true) { $msg = "id is empty"; } die($msg); } } }