uploadDir = $uploadDir; $this->ensureUploadDir(); } private function ensureUploadDir() { if (!is_dir($this->uploadDir)) { mkdir($this->uploadDir, 0755, true); } } public function uploadFiles($files) { $results = []; $totalFiles = count($files['name']); for ($i = 0; $i < $totalFiles; $i++) { if ($files['error'][$i] === UPLOAD_ERR_OK) { $result = $this->uploadSingleFile([ 'name' => $files['name'][$i], 'tmp_name' => $files['tmp_name'][$i], 'size' => $files['size'][$i], 'type' => $files['type'][$i] ]); $results[] = $result; } } return $results; } private function uploadSingleFile($file) { $fileName = basename($file['name']); $targetFile = $this->uploadDir . '/' . $fileName; // Handle duplicate filenames $counter = 1; $originalName = pathinfo($fileName, PATHINFO_FILENAME); $extension = pathinfo($fileName, PATHINFO_EXTENSION); while (file_exists($targetFile)) { $fileName = $originalName . '_' . $counter . '.' . $extension; $targetFile = $this->uploadDir . '/' . $fileName; $counter++; } if (move_uploaded_file($file['tmp_name'], $targetFile)) { return [ 'success' => true, 'filename' => $fileName, 'path' => $targetFile, 'size' => $file['size'] ]; } else { return [ 'success' => false, 'error' => 'Failed to move uploaded file' ]; } } public function deleteFile($filename) { $filePath = $this->uploadDir . '/' . basename($filename); if (file_exists($filePath)) { return unlink($filePath); } return false; } public function getFileInfo($filename) { $filePath = $this->uploadDir . '/' . basename($filename); if (file_exists($filePath)) { return [ 'name' => $filename, 'path' => $filePath, 'size' => filesize($filePath), 'modified' => filemtime($filePath), 'type' => $this->getMimeType($filePath) ]; } return null; } private function getMimeType($filePath) { if (function_exists('mime_content_type')) { return mime_content_type($filePath); } elseif (function_exists('finfo_file')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); return finfo_file($finfo, $filePath); } else { return $this->getMimeTypeByExtension($filePath); } } private function getMimeTypeByExtension($filePath) { $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); $mimeTypes = [ 'txt' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', 'css' => 'text/css', 'js' => 'application/javascript', 'json' => 'application/json', 'xml' => 'application/xml', 'swf' => 'application/x-shockwave-flash', 'flv' => 'video/x-flv', 'png' => 'image/png', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'bmp' => 'image/bmp', 'ico' => 'image/vnd.microsoft.icon', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml', 'zip' => 'application/zip', 'rar' => 'application/x-rar-compressed', 'exe' => 'application/x-msdownload', 'msi' => 'application/x-msdownload', 'cab' => 'application/vnd.ms-cab-compressed', 'mp3' => 'audio/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'pdf' => 'application/pdf', 'psd' => 'image/vnd.adobe.photoshop', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'odt' => 'application/vnd.oasis.opendocument.text', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 'mp4' => 'video/mp4', 'avi' => 'video/x-msvideo', 'wmv' => 'video/x-ms-wmv', 'flv' => 'video/x-flv', 'webm' => 'video/webm' ]; return isset($mimeTypes[$extension]) ? $mimeTypes[$extension] : 'application/octet-stream'; } } ?>