Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
maximgofman authored Jan 6, 2022
1 parent 0190a21 commit 521f525
Show file tree
Hide file tree
Showing 23 changed files with 1,487 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace app;

class login
{
protected $username;
protected $password;
protected $mysqlConnection;


public function __construct($username, $password , $mysqlConnection)
{
$this->password = $password;
$this->username = $username;
$this->mysqlConnection = $mysqlConnection;
}



public function login()
{
// new ist das was ich aus der datenbank bekomme
$query = "SELECT * FROM `useraccounts` WHERE `username` LIKE '$this->username'";
$result = mysqli_query($this->mysqlConnection, $query);
$check = mysqli_fetch_array($result);
if(password_verify($this->password, $check[2])) {
return true;
}else{
return false;
}
}

}

25 changes: 25 additions & 0 deletions app/register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace app;

class register
{
protected $name;
protected $password;
protected $email;
protected $mysqlconnection;

public function __construct($name, $password, $email, $mysqlconnection)
{
$this->name = $name;
$this->password = $password;
$this->email = $email;
$this->mysqlconnection = $mysqlconnection;
}
public function newUser()
{
$createHashPassword = password_hash($this->password, PASSWORD_DEFAULT);
return $this->mysqlconnection->query("INSERT INTO `useraccounts` (`ID`, `username`, `password`, `email`) VALUES (NULL, '$this->name', '$createHashPassword', '$this->email')");
}

}
28 changes: 28 additions & 0 deletions app/upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace app;

class upload
{
protected $id;
protected $currentfile;
protected $file;
protected $mysqlconnection;


public function __construct($id, $currentfile,$file, $mysqlconnection)
{
$this->id = $id;
$this->file = $file;
$this->mysqlconnection = $mysqlconnection;
$this->currentfile = $currentfile;
}


public function upload()
{

return $this->mysqlconnection->query("UPDATE `userdata` SET `data` = 'test' WHERE `userdata`.`userid` = $this->id");
}

}
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "maximgofman/htdocs",
"autoload": {
"psr-4": {
"app\\": "app/"
}
},
"require": {}
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions fileUploadService/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<html>
<head>


<title>SimpleFileUpload</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
body{
display:flex;
justify-content: center;
}
.upload-box{
background-color: rgba(255,255,255,1);
display:flex;
}
</style>

übungsaufgaben
</head>

<body>
<div class="container">
<div class="row justify-content-center align-items-center">

<?php
if(isset($_GET['upload'])){
if($_GET['upload'] == "wrongtype"){
echo "Diese Datei darf nicht hochgeladen werden!";
}else if($_GET['upload'] == "success"){
echo "Datei wurde erfolgreich hochgeladen";
}else if($_GET['upload'] == "errorsize"){
echo "Datei ist viel zu groß!";
}
}else if(isset($_GET['del'])){
$status = $_GET['del'];
if($status == "error"){
echo "Datei konnte nicht gelöscht werden";
}else if($status == "success"){
echo "Datei wurde erfolreich gelöscht";
}
}



$verzeichnis = openDir("uploads");


while($file = readDir($verzeichnis)){

if($file != ".." && $file != "."){
echo "<div class='mt-2 upload-box'><a href='uploads/$file'>".$file. "</a><br><a href='upload.php?del=uploads/$file'
class='text-danger'>(X)</a></div><br>";
}
}
?>

<form action="upload.php" method="post" enctype="multipart/form-data"> <!-- damit können wir auch bilder verschicken durch die
php datei -->
<input type="file" name="file" class="form-control-file"><br>
<button type="submit" name="submit" class="btn btn-primary mt-2">Upload</button>
</form>
</div>
</div>
</body>
</html>
47 changes: 47 additions & 0 deletions fileUploadService/upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
if(isset($_POST['submit'])){
$file = $_FILES['file'];// FILES kriegt alles was gesubmitet wurde

$fileName = $_FILES['file']['name'];
$fileTMP = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$allowed = array('jpg', 'pdf', 'png', 'json', 'php');
$allowedFileSize = 15000;
$allowedFileSize_mb = $allowedFileSize/1000;
$fileSize_mb = $fileSize/1000000;

if(in_array($fileExt[1], $allowed)){
if($fileError == 0){
if($fileSize_mb < $allowedFileSize_mb) {
$new_file_name = uniqid('', true).".".$fileExt[1];
$fileDestination = "uploads/$new_file_name"."_$fileName";
move_uploaded_file($fileTMP, $fileDestination);
header("location: index.php?upload=success");
}else{
header("location: index.php?upload=errorsize");
echo "zu groß die Datei es ist nur ". $allowedFileSize_mb. "MB erlaubt";
echo "<br>"."Deine datei hat: ".$fileSize_mb."MB";
}
}else {
echo "Probleme beim uploaden";
}

}else {
header("location: index.php?upload=wrongtype");
}

}else if(isset($_GET['del'])){
$dateiname = $_GET['del'];
if(!unlink($dateiname)){
header('Location: index.php?del=error');
}else{
header('Location: index.php?del=success');
}



}
?>
30 changes: 30 additions & 0 deletions login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
if(isset($_GET['loggedOut'])){
if($_GET['loggedOut'] == "sessionExpired"){
echo "Sitzung abgelaufen";
}
}else if(isset($_GET['loginStatus'])){
if($_GET['loginStatus'] == "fail"){
echo "Nutzer konnte nicht eingeloggt werden. Password oder Benutzername exisitiert nicht!";
}else if($_GET['loginStatus'] == "logout"){
echo "Sitzung wurde beendet, Nutzer wurde erfolgreich ausgeloggt!";
}else if($_GET['loginStatus'] == "newuser"){
echo "Neuer Nutzer wurde erfolgreich angelegt, sie können sich jetzt einloggen";
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="src/login.php" method="post">
<label>Username: </label><input type="text" name="username"><br>
<label>Password: </label><input type="password" name="password"><br>
<button type="submit" name="submit">Einloggen</button>
</form>
<a href="regist.php">
Registrieren
</a>
</body>
</html>
36 changes: 36 additions & 0 deletions regist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
require_once('vendor/autoload.php');
require_once('sqlconnect.php');

use app\test;
use app\register;

if(isset($_POST['submit'])){
$regist = new register($_POST['username'], $_POST['password'], $_POST['email'], $mysqli);
$regist->newUser();
header("Location: login.php?loginStatus=newuser");
}


?>

<html>
<head>
<title>Registration</title>
</head>
<body>
<form action="regist.php" method="post">
<label>Username: </label><input type="text" name="username" placeholder="username"><br>
<label>Password: </label><input type="password" name="password" placeholder="password"><br>
<label>E-mail: </label><input type="email" name="email" placeholder="youremail"><br>
<button type="submit" name="submit">Register</button>
</form>
<a href="login.php">einloggen</a>
</body>
</html>






17 changes: 17 additions & 0 deletions sqlconnect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$db_host = 'localhost';
$db_user = 'root';
$db_password = 'root';
$db_db = 'uploadyourdata';

$mysqli = @new mysqli(
$db_host,
$db_user,
$db_password,
$db_db
);

if ($mysqli->connect_error) {
exit();
}
12 changes: 12 additions & 0 deletions src/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
require_once ('../vendor/autoload.php');
require_once ('../sqlconnect.php');
use app\login;

if(((new login($_POST['username'], $_POST['password'], $mysqli))->login())){
session_start();
$_SESSION['username'] = $_POST['username'];
header("Location: privateSpace.php?loginStatus=success");
}else{
header("Location: ../login.php?loginStatus=fail");
}
Loading

0 comments on commit 521f525

Please sign in to comment.