File Uploader // Subir archivo

/** * Upload a file in a specific folder * Optional: you can give optional parameters like extension(s), size or mimetype(s) * * @param array $file * @param string $path * @param array $options [ file_extensions array, file_size numeric bytes, newfile_mimetype array, file_prefix string => 3 letters ] */ public function uploadFile($file, $path = "images/uploads", $options = false){ $this->file = $file; $this->path = $path; $this->options = $options; // Custom fields $this->notcomplete[] = false; $this->imagensize = 10311680; // Default: 5Mb if(is_array($this->file) && is_string($path) && strlen($path) > 5): // Checking if $festination exist if(file_exists($path)): // Setting params $filename = $this->file['name']; $tmp_name = $this->file['tmp_name']; $filesize = (int)$this->file['size']; $filetype = strtolower($this->file['type']); $error = $this->file['error']; if(!empty($filename) && $error == 0 ): // Optional values $newfile_extension = strtolower($options['file_extensions']); $newfile_size = (int)$options['file_size']; $newfile_mimetype = strtolower($options['file_mimetype']); $newfile_prefix = $options['file_prefix']; // Checking imagen upload $extensions = (is_array($newfile_extension) && count($newfile_extension) > 0) ? $newfile_extension : array("gif", "jpeg", "jpg", "png"); $name_explode = explode(".", $filename); $name_end = end($name_explode); $file_extension = strtolower($name_end); $maxSize = (is_numeric($newfile_size) && $newfile_size >= 1) ? $newfile_size : $this->imagensize; $file_mimetype = (is_array($newfile_mimetype) && count($newfile_mimetype) >= 1) ? $newfile_mimetype : array("image/jpeg", "image/jpg", "image/x-png", "application/pdf", "image/png"); // Checking this->file extension if($filesize <= $maxSize && in_array($file_extension, $extensions)): if (in_array($filetype, $file_mimetype)): $cleanString = Genericas::cleanString($filename); $filename = time()."_".$cleanString; $filename = (is_string($newfile_prefix) && strlen($newfile_prefix) >= 3) ? $newfile_prefix . "_" . $filename : $filename; $path = $path ."/". $filename; $up = move_uploaded_file($tmp_name, $path); if($up): //unlink($pathForUp . $val["sArchivo"]["name"]); $success = array(true, $filename); return $success; exit; else: // Upload error $notcomplete[] = "upload"; endif; else: // Error with this->file type $notcomplete[] = "file_type"; endif; else: // Error with filesize $notcomplete[] = "file_size"; endif; else: // Empty filename or error > 0 $notcomplete[] = "filename_or_error"; endif; else: // Path doesn't exist $notcomplete[] = "path"; endif; else: // Destination doesn't exist $notcomplete[] = "file"; endif; return $notcomplete; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.