Skip to main content

Server

User and Folder env

Create user and set the folder permission with write only so that uploaded file is not readable.

# useradd [user]
# mkdir [folder]
# chown [user] [folder]
# cd [folder]
# su - [user]
$ umask 555

After executing umask command the folder has write permission only [222]

PHP Server

PHP Command to start the server.

php -S <ip-address:port> -t <directory-path>

PHP file uploads - short form

Create a file with upload.php in a directory you want to serve.

<?php
$fname = basename($_REQUEST["filename"]);
file_put_contents('upload/' . $fname, file_get_contents('php://input'));
?>

PHP file upload - with html

HTML file:

<!DOCTYPE html>
<html>
<body>

<form action="uploads.php" method="post" enctype="multipart/form-data">
Select the file:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>

</body>
</html>

Create uploads sub folder and php file upload script uploads.php:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if $uploadOk is set to 0 by an error
if(isset($_POST["submit"])) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

python HTTP server

python2 server

python2 -m SimpleHTTPServer 80

python3 server

python3 -m http.server 80

Ruby http server

Serve default port

ruby -run -e httpd
note

Defaults to current directory
Serves on 0.0.0.0:8080
directory listing if there is no index.html

Serve specific port and folder

ruby -run -e httpd /path/to/server-folder -p 9090

Generate OpenSSL cert

openssl req -newkey rsa:2048 -nodes -keyout privkey.pem -x509 -days 36500 -out certificate.pem

Ref:

Devdungeon - good read and has openssl