File size: 5,453 Bytes
a922466 b908402 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <?php
// utils.php - Additional utility functions
class FileManager {
private $uploadDir = 'uploads';
public function __construct($uploadDir = 'uploads') {
$this->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';
}
}
?> |