Upload files to separate folders in the database

Asked

Viewed 121 times

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");

1 answer

0


Hello, my dear. Answering your question:

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?

In my view both. I would create a table in the bank, with the name documents, with some columns more or less like this (doc_id, doc_nome, doc_tipo, user_id) and would relate to the users table by user_id. About the folders I already made this mistake of creating the folders for each user, from the lot of work to do the maintenance of this, the solution was to rename the file in the upload with a pattern, where it was easy to identify from whom it was.

The name formation was the data do dia + valor aleatório + user_id, would look something like this 20200922-KASO78445589554-11.pdf, and saves everything in a folder. You can improve this nomenclature by using encryption functions.

  • 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

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.