96 lines
4.3 KiB
PHP
96 lines
4.3 KiB
PHP
<?php
|
||
namespace App\Controller;
|
||
|
||
session_start();
|
||
use App\Veiw\View;
|
||
use Shalex\Engine\Db;
|
||
|
||
class UploadController
|
||
{
|
||
public function actionIndex()
|
||
{
|
||
if (isset($_SESSION['IS_AUTH']) && $_SESSION['IS_AUTH'] == true) {
|
||
$portial = (View::render([], 'upload_tpl.php'));
|
||
echo (View::render(['content' => $portial], 'layout_admin.php'));
|
||
} else {
|
||
header("Location:http://localhost:8000/login");
|
||
}
|
||
}
|
||
|
||
public function actionAddfile()
|
||
{
|
||
try {
|
||
$link = Db::connection(CONFIG_DB);
|
||
} catch (\Doctrine\DBAL\Exception $e) {
|
||
die("Ошибка подключения к базе данных: " . $e->getMessage());
|
||
}
|
||
|
||
$uploaddir = 'C:\\Lerning\\lerning-php\\itmo-php-course\\engine\\uploads\\';
|
||
$fileName = basename($_FILES['userfile']['name']);
|
||
$uploadfile = $uploaddir . $fileName;
|
||
|
||
$fileMimeType = mime_content_type($_FILES['userfile']['tmp_name']);
|
||
$fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
|
||
|
||
$allowedMimeTypes = ['audio/mpeg'];
|
||
$allowedExtensions = ['mp3'];
|
||
|
||
$maxFileSize = 11 * 1024 * 1024;
|
||
|
||
if (
|
||
in_array($fileMimeType, $allowedMimeTypes) &&
|
||
in_array($fileExtension, $allowedExtensions) &&
|
||
$_FILES['userfile']['size'] <= $maxFileSize
|
||
) {
|
||
// Проверка наличия файла в базе данных
|
||
try {
|
||
$sql = "SELECT * FROM files WHERE file_name = :file_name";
|
||
$stmt = $link->prepare($sql);
|
||
$stmt->bindValue('file_name', $fileName);
|
||
$result = $stmt->executeQuery();
|
||
$existingFile = $result->fetchAssociative();
|
||
|
||
if ($existingFile) {
|
||
$message = "Файл уже загружен. Ссылка для скачивания: <a href='{$existingFile['download_link']}'>{$existingFile['download_link']}</a>";
|
||
$status = 'info';
|
||
} else {
|
||
// Проверка наличия файла в директории
|
||
if (file_exists($uploadfile)) {
|
||
$message = "Файл с таким именем уже существует на сервере.";
|
||
$status = 'info';
|
||
} else {
|
||
// Перемещение загруженного файла в директорию
|
||
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
|
||
// Генерация ссылки для скачивания
|
||
$downloadLink = './uploads/' . $fileName;
|
||
|
||
// Сохранение ссылки в базу данных
|
||
try {
|
||
$link->insert('files', [
|
||
'file_name' => $fileName,
|
||
'download_link' => $downloadLink,
|
||
]);
|
||
$message = "Файл успешно загружен. Ссылка для скачивания: <a href='$downloadLink'>$downloadLink</a>";
|
||
$status = 'success';
|
||
} catch (\Doctrine\DBAL\Exception $e) {
|
||
$message = "Ошибка при сохранении в базу данных: " . $e->getMessage();
|
||
$status = 'error';
|
||
}
|
||
} else {
|
||
$message = "Ошибка при загрузке файла.";
|
||
$status = 'error';
|
||
}
|
||
}
|
||
}
|
||
} catch (\Doctrine\DBAL\Exception $e) {
|
||
echo "Ошибка при проверке файла в базе данных: " . $e->getMessage();
|
||
return;
|
||
}
|
||
} else {
|
||
$message = "Ошибка: разрешены только MP3-файлы размером до 10 MB.";
|
||
$status = 'error';
|
||
}
|
||
// $rendMessage = (View::render([], 'message_tpl.php'));
|
||
// echo (View::render(['message' => $rendMessage], 'layout_admin.php'));
|
||
}
|
||
} |