Last February, we have reviewed how to setup a Nginx web server with PHP under Haiku in the article named Nginx and PHP.
This setup is perfect in case you need to transfer files from a machine on your local network to your Haiku system.
We will review in this article how to proceed in 5 minutes :)
We will suppose that you have installed both Nginx and PHP on your Haiku system.
If it's the case, when you open WebPositive on the below URL, you will see the Nginx welcome page displayed:
https://127.0.0.1
Now let's verify as well that PHP is recognized.
Create the below PHP page in the directory of your web server :
cd /boot/home/www
lpe info.php
Copy and paste the below content:
<?php
phpinfo();
?>
Change the owner and group to "nginx":
chown nginx:nginx info.php
Then call this PHP info page :
http://127.0.0.1/info.php
Ok the pre-requisites are now fine!
In order to have the possibility to upload files via your Nginx webserver located on your Haiku system, open a Terminal and edit the "haiku.php" page :
cd www
lpe haiku.php
Copy and paste the below script:
<?php
// Upload destination directory
$uploadDir = __DIR__ . '/uploads/';
$messages = [];
// Create the directory if it doesn't exist
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
// Handle file uploads
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['files'])) {
foreach ($_FILES['files']['tmp_name'] as $index => $tmpName) {
$filename = basename($_FILES['files']['name'][$index]);
if (is_uploaded_file($tmpName)) {
$destination = $uploadDir . $filename;
if (move_uploaded_file($tmpName, $destination)) {
$messages[] = "<div class='alert alert-success'>File <strong>$filename</strong> uploaded successfully.</div>";
} else {
$messages[] = "<div class='alert alert-danger'>Error saving <strong>$filename</strong>.</div>";
}
} else {
$messages[] = "<div class='alert alert-warning'>File <strong>$filename</strong> was not uploaded (temporary issue).</div>";
}
}
// Redirect back to the form after 3 seconds
header("Refresh: 3; url=" . $_SERVER['PHP_SELF']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload Files to Haiku</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container py-5">
<h1 class="mb-4">Upload Files to Haiku</h1>
<?php if (!empty($messages)): ?>
<div class="mb-4">
<?php echo implode('', $messages); ?>
<p class="text-muted">Redirecting in 3 seconds…</p>
</div>
<?php endif; ?>
<form method="post" enctype="multipart/form-data" class="card p-4 shadow-sm">
<div class="mb-3">
<label class="form-label">Select one or more files</label>
<input type="file" name="files[]" class="form-control" multiple required>
</div>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
</div>
</body>
</html>
Save the file.
Change the owner and group to nginx, and create a directory named "uploads" :
chown nginx:nginx haiku.php
mkdir uploads
chown nginx:nginx uploads
Last step : verify the local IP address corresponding to Haiku:
ifconfig | grep inet
Note this IP address.
Open a web browser from your other machine to call the "haiku.php" page :
http://192.168.64.29/haiku.php
If everything is working fine, you will see the main upload page:
Click on "Select files".
You can multi select the corresponding files you would like to transfer :
Click on the "Transfer" button.
In the main page, you should see the number of files to transfer (3 in the below example).
Proceed by clicking the "Upload" button:
The page will confirm each file transferred as per below:
After a timeout of 3 seconds, it will redirect you to the main page in case you need to transfer another set of files.
Now go into Haiku and verify the content of your "/boot/www/uploads" folder:
Nice!
All files were transferred successfully:)
In case you need to transfer from Haiku to your machine, you can apply the same setup:
Install a web server on the destination machine with the same PHP upload page, and then call this PHP page from your Haiku system.
Happy files transfer from / to Haiku :)