For all package download and installation, I will do that under virtual environment to manage dependencies without affecting the system Python installation. The detail of creating a virtual environment can be found in my previous blog
https://cs-student.com/virtual-environment-in-raspberry-pi/
Step 1: Download package for Raspberry Pi
Open a terminal and run the following commands to ensure your system is up to date:
sudo apt update
sudo apt upgrade
Install the Apache web server:
sudo apt install apache2
Install PHP, to handle download:
sudo apt install php libapache2-mod-php
Step 2: Configuration
The default web directory is /var/www/html. Create a directory for the downloadable files:
sudo mkdir /var/www/html/downloads
Set permissions for the download directory:
sudo chown -R www-data:www-data /var/www/html/downloads
Place any files available for download in the /var/www/html/downloads
directory SCP, FTP, or directly on the Raspberry Pi.
Create a new PHP script to handle generating unique download links:
sudo nano /var/www/html/generate_link.php
Codes:
<?php
session_start();
// List of files with unique identifiers
$files = [
"file1.zip" => "file1_unique_id",
"file2.zip" => "file2_unique_id",
// Ensure you have actual files in the 'downloads' directory
];
// Function to generate a random string
function generateRandomString($length = 8) {
return substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')))), 1, $length);
}
// Check if a file is requested
if (isset($_GET['file'])) {
$randomString = $_GET['file'];
if (isset($_SESSION[$randomString])) {
$uniqueId = $_SESSION[$randomString];
$originalFile = array_search($uniqueId, $files);
if ($originalFile && file_exists("downloads/$originalFile")) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($originalFile) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize("downloads/$originalFile"));
readfile("downloads/$originalFile");
exit;
} else {
echo "File not found.";
}
} else {
echo "Invalid download link.";
}
} else {
$randomString = generateRandomString();
if (empty($files)) {
echo "No files available for download.";
exit;
}
$fileCount = count($files);
$fileIndex = rand(0, $fileCount - 1);
$fileName = array_keys($files)[$fileIndex];
$uniqueId = $files[$fileName];
if (!empty($fileName) && !empty($uniqueId)) {
$_SESSION[$randomString] = $uniqueId;
echo "Your download link: <a href='generate_link.php?file=" . htmlspecialchars($randomString) . "'>" . "http://192.168.2.230/" . htmlspecialchars($randomString) . "</a>";
} else {
echo "Error: Selected file or unique ID is empty.";
}
}
?>
Navigate to http://192.168.2.230/generate_link.php
. This will generate a unique download link for the file such as .
Your download link: http://192.168.2.230/Uyb8N7Lf
When the generated link is clicked, it take me to the appropriate file for download.