<?php
function saveFile($file, $path) {
$count = 0;
$originalFileName = $file['name'];
$fileName = $originalFileName;
while (file_exists($path . '/' . $fileName)) {
$count++;
$fileName = pathinfo($originalFileName, PATHINFO_FILENAME) . '(' . $count . ').' . pathinfo($originalFileName, PATHINFO_EXTENSION);
}
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
move_uploaded_file($file['tmp_name'], $path . '/' . $fileName);
if (file_exists($path . '/' . $fileName)) {
return $fileName;
} else {
return false;
}
}
/**
* How to USE
*/
/*
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$uploadDir = 'uploads';
$uploadedFile = $_FILES['file'];
$savedFileName = saveFile($uploadedFile, $uploadDir);
// Here you can save the information to a DataBase
}
*/
|