public function upload(Request $request) { Log::info('Uploading Files!'); $file = $request->file; $extension = $file->getClientOriginalExtension(); Storage::disk('local')->put($file->getFilename() . '.' . $extension, File::get($file)); $entry = new Fileentry(); $entry->mime = $file->getClientMimeType(); $entry->original_filename = $file->getClientOriginalName(); $entry->filename = $file->getFilename() . '.' . $extension; $entry->model_id = "0"; //create thumb for images if (strpos($file->getClientMimeType(), "image/") !== false) { $image = new ImageResize($file); $image->resizeToHeight(150); $entry->thumb = $image->getImageAsString(); } $entry->save(); return ['success' => false, 'data' => 200]; }
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); } } }