Hack Forums
upload multiple files and keep previous files like github do PHP/MYSQL - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: upload multiple files and keep previous files like github do PHP/MYSQL (/showthread.php?tid=6199268)



upload multiple files and keep previous files like github do PHP/MYSQL - blackcoder00 - Mar 29, 2022

Hi hackforums family , my problem is that i want to upload multiple files (php/html/css etc..) and keep my old version of files like github does, like to upload new files and when i want go back to my olds files i find it and i can download it i searched in the forums if there's someone that asked about this topic to help me and i didn't find anything the main idea is that i can upload files (html/css/sql etc..) and when i want to upload new files i can anytime go back and find the old version of files like github exactly
PHP Code

<?php 
<form method="POST" action="php/upload.php" enctype="multipart/form-data">
    <
div class="control-panel">
        <
label>Project folder name</label>
      <
input type="text" name="directoryName"><br>
      <
label>Project name</label>
      <
input type="text" name="projectName"><br>
      <
label>Project description</label>
      <
textarea name="description"></textarea><br>

      <
span>PUSH PROJECT</span>
      <
input type="file" name="uploadedFile" />
    </
div>

    <
input type="submit" class="uploadBtn button5" name="uploadBtn" value="Push" />
  </
form> 


PHP Code

<?php
session_start
();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "github";
$description = $_POST["description"];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die(
"Connection failed: " . $conn->connect_error);
}
$user = $_SESSION["username"];
$message = ''; 
echo 
"hello";
if (isset(
$_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Push')
{
  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 = (date("Y-m-d")."_".time()."_". $fileName) . '.' . $fileExtension;

    
// check if file has one of the following extensions
    
$allowedfileExtensions = array('jpg', 'php', 'html', 'zip', 'txt', 'xls', 'doc');

    if (
in_array($fileExtension, $allowedfileExtensions))
    {
      
// directory in which the uploaded file will be moved
      
$dirc = $_POST["directoryName"];
      
mkdir($dirc);
      
$uploadFileDir = './'.$dirc.'/';
      
$dest_path = $uploadFileDir . $newFileName;
      
$projectName=$_POST["projectName"];
      
$sql = "INSERT INTO project
VALUES ('
$user', '$dest_path','$description','$projectName')";

if (
$conn->query($sql) === TRUE) {
  echo 
"New record created successfully";
} else {
  echo 
"Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
      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;
//begin *********************************************************************




header("Location: ../"); 



RE: upload multiple files and keep previous files like github do PHP/MYSQL - Nachfolger - Mar 29, 2022

Are you trying to show a comparison view, or just have an index (log) of file revisions?

Your code is not secure. Among the many issues, I'm scared by the fact that you're allowing users to choose where the file is stored on your server... Also, your error messages give way too much background detail; why should the end user know the exact database connection error message? Last, but far from least, why are you not using prepared statements? I know for a fact that I have told you a million times to switch.

I wrote this post a long time ago, but you would benefit from actually listening to what it has to say. See below:
(Sep 11, 2019 09:21 AM)Nachfolger Wrote:  [...]

File Upload examples:
-- https://www.w3schools.com/php/php_file_upload.asp
-- https://www.tutorialspoint.com/php/php_file_uploading.htm

[...]

Some things to keep in mind when handling uploads:
-- Store files out of the root directory, above public access
-- Validate file mime (Using a mime reader, not the browser's provided mime) (Ex. https://gist.github.com/Erutan409/8e774dfb2b343fe78b14#file-mimetype-php)
-- Rename files using md5_file or sha1_file (Prevents duplicate files, standardizes the file names stored on the server)
-- Ensure the file was a uploaded file (https://www.php.net/manual/en/function.is-uploaded-file.php)
-- Use a file type whitelist (Not blacklist), and validate the established mime with the whitelist
-- Disable the PHP engine and all other backend parsers from running within the upload directory
-- Disable direct access to upload directory ("Required all denied", etc)

Beyond the short list given here, do your own research and find anything I missed. Regardless of whether this project is just for you or not, the goal (I would assume) is to establish basic principles when working with PHP. [...]



RE: upload multiple files and keep previous files like github do PHP/MYSQL - SIM-2502833 - Mar 29, 2022

Could use something like gitlabs.


RE: upload multiple files and keep previous files like github do PHP/MYSQL - blackcoder00 - Mar 30, 2022

(Mar 29, 2022 05:16 PM)Nachfolger Wrote:  Are you trying to show a comparison view, or just have an index (log) of file revisions?

Your code is not secure. Among the many issues, I'm scared by the fact that you're allowing users to choose where the file is stored on your server... Also, your error messages give way too much background detail; why should the end user know the exact database connection error message? Last, but far from least, why are you not using prepared statements? I know for a fact that I have told you a million times to switch.

I wrote this post a long time ago, but you would benefit from actually listening to what it has to say. See below:

i want to upload many files of a project and if im gonna upload new file and i could reach the old one anytime exemple
if im gonna upload (index.html / search.php)
if im gonna update the code of search.php and upload it again , i can anytime reach the old file
like :
upload : search.php (for the first time)
next if i want to update the code of search.php and upload it again
i can reach the old file anytime like the github does


RE: upload multiple files and keep previous files like github do PHP/MYSQL - Nachfolger - Mar 31, 2022

(Mar 30, 2022 09:28 AM)blackcoder00 Wrote:  i want to upload many files of a project and if im gonna upload new file and i could reach the old one anytime exemple
if im gonna upload (index.html / search.php)
if im gonna update the code of search.php and upload it again , i can anytime reach the old file
like :
upload : search.php (for the first time)
next if i want to update the code of search.php and upload it again
i can reach the old file anytime like the github does

It looks like this project is for deploying changes to your own website, is there a reason why you're not using a git service for this?


RE: upload multiple files and keep previous files like github do PHP/MYSQL - blackcoder00 - Mar 31, 2022

(Mar 31, 2022 04:50 AM)Nachfolger Wrote:  It looks like this project is for deploying changes to your own website, is there a reason why you're not using a git service for this?

it's a university project that i need to clone the github website functionality ( i need to create the same ecosystem of github)
that when i upload some file of a project (php/html etc...) and after that i update the code of some file of that project and upload it again i can reach the previous file ( old one ) anytime i want like github does i need to do it with php exemple :
1)upload (index.html)
after that i want to modify some line in index.html
2)upload it again
the new file it gonna replace the old one in the website and display
and if i want to go back and dowlnload the old file that i modified i can do it (like github does)
this is the first time that we talk about the ecosystem of github in the forums i hope that someone can help me to build it with php


RE: upload multiple files and keep previous files like github do PHP/MYSQL - Disease - Apr 1, 2022

This isn't what Git does. I understand the confusion, but version control is a different concept entirely. If you want to have the benefit of Git, you could always set up a CI/CD pipeline. But I don't think that's what you're actually trying to accomplish here. Sounds more like you're attempting to retain all uploads.

To do that you'll want to check if that file already exists when you are executing the upload. And if it does, either upload the new file with a new name, or rename the previous file(s).