Add files via upload

This commit is contained in:
Getindor 2020-09-10 00:44:14 -04:00 committed by GitHub
parent b6ae0c6088
commit d709e77ab3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 193 additions and 0 deletions

39
file.php Normal file
View File

@ -0,0 +1,39 @@
<?php
$file = $_GET["q"];
$ext = pathinfo($file, PATHINFO_EXTENSION);
switch(strtolower($ext)){
case "txt":
case "php":
case "php5":
case "phtml":
case "css":
header("Content-Type: text/plain");
break;
case "webm":
header("content-Type: video/webm");
break;
case "ogg":
header("content-Type: audio/ogg");
break;
case "webp":
header("content-Type: image/webp");
break;
case "png":
header("content-Type: image/png");
break;
case "gif":
header("content-Type: image/gif");
break;
case "jpeg":
case "jpg":
header("content-Type: image/jpeg");
break;
default:
header("content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . '"' . $file . '"');
break;
}
echo file_get_contents("uploads/src/" . $file . "-download");
?>

32
index.php Normal file
View File

@ -0,0 +1,32 @@
<html>
<head>
<title>FileDrive</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container-fluid h-100 d-inline-block">
<div class="row h-100">
<div id="sidebar" class="col-sm- h-100 d-inline-block">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input id="fileTitle" name="fileTitle" type="text" placeholder="File Title" class="form-control" maxlength="50" value="" autocomplete="off">
<input id="fileAttachment" name="fileAttachment" type="file" class="form-control-file">
<div class="text-center">
<label for="fileAttachment" id="fileAttachmentLabel">
<img src="static/upload2.png" style="height:100px;">
</label>
</div>
<input id="submit" type="submit" value="Upload File" placeholder="File Title" class="form-control">
</form>
</div>
<div class="col" id="list">
<?php
include("list.php");
?>
</div>
</div>
</div>
</body>
</html>

26
list.php Normal file
View File

@ -0,0 +1,26 @@
<?php
function makeRow($title, $ext, $date, $download){
$whitelist = ["png", "gif", "jpg", "jpeg", "webm", "webp", "txt", "php", "php5", "phtml"];
$imagelist = ["png", "gif", "jpg", "jpeg", "webm", "webp"];
if(in_array($ext, $whitelist)){
return "<tr>" . "<td scope='col'>" . $title . "</td>" . "<td scope='col'>" . $ext . "</td>" . "<td scope='col'>" . $date . "</td>" . "<td scope='col'>" . "<a href='file.php?q=" . substr($download, 12, -9) . "'>View</a> " . "</td>" . "</tr>";
} else {
return "<tr>" . "<td scope='col'>" . $title . "</td>" . "<td scope='col'>" . $ext . "</td>" . "<td scope='col'>" . $date . "</td>" . "<td scope='col'>" . "<a href='file.php?q=" . substr($download, 12, -9) . "'>Download</a> " . "</td>" . "</tr>";
}
}
$files = json_decode(file_get_contents("uploads/files.json"));
?>
<table class="table">
<thead>
<th scope="col">Title.</th>
<th scope="col">Ext.</th>
<th scope="col">Date.</th>
<th scope="col">Links.</th>
<?php
foreach($files as $file){
echo makeRow($file->title, $file->ext, $file->date, $file->download);
}
?>
</thead>
</table>

BIN
static/background.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

BIN
static/upload.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
static/upload2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

40
style.css Normal file
View File

@ -0,0 +1,40 @@
body{
background:url("static/background.png");
}
#sidebar{
background:#222;
padding:10px;
}
#submit{
background:#333;
color:#eee;
}
#fileTitle{
background:#333;
color:#eee;
}
#fileAttachmentLabel{
height:200px;
padding-top:50px;
cursor:pointer;
}
#fileAttachment{
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
#sort{
border-top:1px solid #ddd;
border-bottom:1px solid #ddd;
padding:2px;
color:ddd;
text-align:center;
}
#list{
height:100%;
overflow-y: scroll;
}

55
upload.php Normal file
View File

@ -0,0 +1,55 @@
<?php
$whitelist = ["png", "jpeg", "jpg", "cpp", "txt", "gif"];
$ext = pathinfo($_FILES["fileAttachment"]["name"], PATHINFO_EXTENSION);
//$path = "uploads/src/" . basename($_FILES["fileAttachment"]["name"]);
$path = "uploads/src/" . uniqid() . "." . $ext . "-download";
//Check if file is valid
function fileValid($file){
if(empty($file)){
echo "Error: no file specified";
return false;
}
echo $file['name'];
return true;
}
// create file object
function indexFile($ext, $path){
$title = htmlspecialchars($_POST["fileTitle"]);
if(empty($title)){
$title = "Untitled";
}
$file = array(
"title" => substr($title, 0, 50),
"ext" => $ext,
"date" => date("Y/m/d g:i:s"),
"download" => $path
);
// write to files.json
$file_list = fopen("uploads/files.json", "c+");
if(flock($file_list, LOCK_EX)){
$db = json_decode(fread($file_list, filesize("uploads/files.json")));
print_r($db);
array_unshift($db, $file);
ftruncate($file_list, 0);
rewind($file_list);
fwrite($file_list, json_encode($db));
fflush($file_list);
flock($file_list, LOCK_UN);
} else {
echo "Couldn't get the lock. Please try your upload again.";
}
fclose();
}
//upload file
if(fileValid($_FILES["fileAttachment"])){
if(move_uploaded_file($_FILES["fileAttachment"]["tmp_name"], $path)){
echo "File upload successful";
indexFile($ext, $path);
} else{
echo "Upload failed.";
}
}
?>

1
uploads/files.json Normal file
View File

@ -0,0 +1 @@
[{"title":"Picture of monkey using a phone.","ext":"png","date":"2020\/09\/09 2:14:23","download":"uploads\/src\/5f591b7f3eda0.png-download"},{"title":"Picture of me creating a PHP application.","ext":"jpg","date":"2020\/08\/09 12:09:33","download":"uploads\/src\/5f2f76fd99e5d.jpg-download"}]

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB