Skip to content

Instantly share code, notes, and snippets.

@dusta
Created July 12, 2018 09:11
Show Gist options
  • Save dusta/dd35245693644dd976b33304b13a5f6c to your computer and use it in GitHub Desktop.
Save dusta/dd35245693644dd976b33304b13a5f6c to your computer and use it in GitHub Desktop.
dframe/filestorage upload file with slug path generator
<?php
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'POST':
if (!isset($_FILES) and !isset($_FILES['attachment']) or !isset($_POST['publication_id']) or empty($_POST['publication_id'])) {
return ($view->renderJSON(array('return' => '0', 'response' => 'Blad formularza')));
}
$publicationId = htmlspecialchars($_POST['publication_id']);
$publicationsModel = $this->loadModel('Publications');
$getPublication = $publicationsModel->getPublication($publicationId);
if ($getPublication['return'] != true) {
return ($view->renderJSON(array('return' => '0', 'response' => 'Nie istnieje taka publikacja.')));
}
$allowedTypes = array('application/pdf', 'image/png', 'image/jpeg');
$allowedExtenstions = array('pdf', 'png', 'jpg');
$errors = 'Pomyślnie dodano publikację, ale nie udało się wgrać wszystkich plików. ';
$success = 0;
foreach ($_FILES as $lp => $file) {
if ($file['error'] != 0) {
$errors .= 'Błąd z plikiem ' . $file['name'];
continue;
}
$attachmentName = $file['name'];
$attachmentSize = $file['size'];
if ($attachmentSize > (1024 * 1024 * 5)) {
$errors .= 'Plik ' . $attachmentName . ' był za duży i nie został wgrany. Maksymalny rozmiar wynosi 5 MB. ';
continue;
}
$extension = strtolower(pathinfo($attachmentName, PATHINFO_EXTENSION)); //Walidacja Rozszerzenia
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file['tmp_name']); //Walidacja Mine
finfo_close($finfo);
if (!in_array($mime, $allowedTypes) or !in_array($extension, $allowedExtenstions)) {
$errors .= 'Plik ' . $attachmentName . ' miał niedozwolony typ i nie został wgrany. ';
continue;
}
$id = md5(uniqid(rand(), true));
$filename = substr($attachmentName, 0, strrpos($attachmentName, "."));
$filename = preg_replace("/[^a-zA-Z0-9_]+/", "", $filename);
$filename = substr($filename, 0, 128);
$uploadPath = 'attachments/' . date('Y') . '/' . date('m') . '/' . date('d') . '/' . substr($id, 0, 2) . '/' . substr($id, 2, 2) . '/' . $filename . '-' . substr($id, 4, 32) . '.' . $extension;
$put = $this->baseClass->fileStorage->put('local', $file['tmp_name'], $uploadPath);
if ($put['return'] == true) {
$addAttachment = $publicationsModel->addAttachment($publicationId, (int)$put['fileId'], $filename);
if ($addAttachment['return'] == true) {
$success++;
continue;
}
}
$errors .= 'Nie udało się wgrać pliku ' . $attachmentName;
}
$status = ($success > 0) ? '1' : '0';
$response = ($errors == 'Pomyślnie dodano publikację, ale nie udało się wgrać wszystkich plików. ') ? 'Pomyślnie dodano publikację i załączniki.' : $errors;
return $view->renderJSON(array('return' => $status, 'response' => $response));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment