0
Good morning, first excuse my noobisse but I’m still learning kkk
I am creating a system (I am using HTML, CSS, Javascript, PHP and Mysql Database), where will have several registered users and each user can upload files in pdf.
My question is what would be the best way to do this? Saving them in the MYSQL database or saving them in a folder on the server?
My initial idea was to save in folder on the server, and that when sending the file was created a folder with the name or identification of the user and the files were saved there, to be easier to identify, however, I have no idea how to do this.
Can someone help me? give me a hint? Thank you!!
I found the following code, it’s very simple, I would have a better way to do this or I can use it? Below is the code:
session_start();
$message = '';
if (isset($_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Upload')
{
if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === UPLOAD_ERR_OK)
{
// get details of the uploaded file
$fileTmpPath = $_FILES['uploadedFile']['tmp_name'];
$fileName = $_FILES['uploadedFile']['name'];
$fileSize = $_FILES['uploadedFile']['size'];
$fileType = $_FILES['uploadedFile']['type'];
$fileNameCmps = explode(".", $fileName);
$fileExtension = strtolower(end($fileNameCmps));
// sanitize file-name
$newFileName = md5(date("m.d.y") . $fileName) . '.' . $fileExtension;
// check if file has one of the following extensions
$allowedfileExtensions = array('jpg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc');
if (in_array($fileExtension, $allowedfileExtensions))
{
// directory in which the uploaded file will be moved
$uploadFileDir = './uploaded_files/';
$dest_path = $uploadFileDir . $newFileName;
if(move_uploaded_file($fileTmpPath, $dest_path))
{
$message ='File is successfully uploaded.';
}
else
{
$message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
}
}
else
{
$message = 'Upload failed. Allowed file types: ' . implode(',', $allowedfileExtensions);
}
}
else
{
$message = 'There is some error in the file upload. Please check the following error.<br>';
$message .= 'Error:' . $_FILES['uploadedFile']['error'];
}
}
$_SESSION['message'] = $message;
header("Location: index.php");
I understood, so I would save all the pdf files in the same folder with an encrypted name and save in the bank his name, the encrypted name, like, path... I found the following one code here, I will post it up there. I think it answers what you said
– user192882
Exactly, the code you thought is good I would only improve this line
$newFileName = md5(date("m.d.y") . $fileName) . '.' . $fileExtension;
, to improve the name formation, the way it is will generate a hash based on the date value and the original name of the file. This hash is very large and does not tell you which user it comes from.– Gnomo Escalate