Apache Upload: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(Die Seite wurde neu angelegt: „Kategorie: Administration = Saubere Installation der Apache2-PHP-Umgebung = Für einen stabilen Betrieb unter Debian-basierten Systemen (Debian, Ubuntu) wird…“) |
|||
| Zeile 1: | Zeile 1: | ||
| − | Kategorie: | + | Kategorie: PHP |
| − | = | + | = Datei-Upload mit PHP unter Apache2 = |
| − | + | Diese Anleitung beschreibt die Einrichtung eines Upload-Skripts direkt im Web-Root /var/www/html/. | |
| − | == | + | == Systemvorbereitung == |
| − | Die | + | Die Berechtigungen werden so gesetzt, dass der Webserver-Nutzer Dateien im Zielverzeichnis ablegen kann. |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
cd /var/www/html | cd /var/www/html | ||
| Zeile 25: | Zeile 14: | ||
chmod -R 755 /var/www/html/ | chmod -R 755 /var/www/html/ | ||
| − | == | + | == Skript-Implementierung (index.php) == |
| − | |||
| − | |||
| − | + | Der folgende Code wird direkt in /var/www/html/index.php gespeichert. Er verarbeitet den Upload und verschiebt die Datei in den Ordner uploads/. | |
| − | + | <?php | |
| + | /** | ||
| + | * PHP-Skript für Datei-Uploads | ||
| + | * Standort: /var/www/html/index.php | ||
| + | */ | ||
| − | + | $upload_dir = 'uploads/'; | |
| − | + | $status_message = ""; | |
| − | == | + | if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file_upload'])) { |
| + | |||
| + | $file = $_FILES['file_upload']; | ||
| + | |||
| + | if ($file['error'] === UPLOAD_ERR_OK) { | ||
| + | $filename = basename($file['name']); | ||
| + | $target_path = $upload_dir . $filename; | ||
| − | + | if (move_uploaded_file($file['tmp_name'], $target_path)) { | |
| + | $status_message = "Datei erfolgreich hochgeladen."; | ||
| + | } else { | ||
| + | $status_message = "Fehler: Datei konnte nicht gespeichert werden."; | ||
| + | } | ||
| + | } else { | ||
| + | $status_message = "Upload-Fehler. Code: " . $file['error']; | ||
| + | } | ||
| + | } | ||
| + | ?> | ||
| − | + | <!DOCTYPE html> | |
| − | + | <html lang="de"> | |
| + | <head> | ||
| + | <meta charset="UTF-8"> | ||
| + | <title>Upload</title> | ||
| + | </head> | ||
| + | <body> | ||
| + | <h3>Datei-Upload</h3> | ||
| + | |||
| + | <?php if ($status_message): ?> | ||
| + | <p><strong><?php echo $status_message; ?></strong></p> | ||
| + | <?php endif; ?> | ||
| − | + | <form action="index.php" method="post" enctype="multipart/form-data"> | |
| − | + | <input type="file" name="file_upload" required> | |
| + | <input type="submit" value="Hochladen"> | ||
| + | </form> | ||
| + | </body> | ||
| + | </html> | ||
| − | + | == Sicherheitseinstellungen == | |
| − | |||
| − | + | Um die Ausführung von Skripten im Upload-Verzeichnis zu verhindern, wird dort eine .htaccess-Datei erstellt. | |
| − | + | cd /var/www/html/uploads | |
| + | printf "php_flag engine off\nOptions -ExecCGI" > .htaccess | ||
| − | [[Kategorie: | + | [[Kategorie:Programmierung]] |
[[Kategorie:Apache]] | [[Kategorie:Apache]] | ||
[[Kategorie:PHP]] | [[Kategorie:PHP]] | ||
Version vom 17. März 2026, 12:04 Uhr
Kategorie: PHP
Datei-Upload mit PHP unter Apache2
Diese Anleitung beschreibt die Einrichtung eines Upload-Skripts direkt im Web-Root /var/www/html/.
Systemvorbereitung
Die Berechtigungen werden so gesetzt, dass der Webserver-Nutzer Dateien im Zielverzeichnis ablegen kann.
cd /var/www/html mkdir uploads chown -R www-data:www-data /var/www/html/ chmod -R 755 /var/www/html/
Skript-Implementierung (index.php)
Der folgende Code wird direkt in /var/www/html/index.php gespeichert. Er verarbeitet den Upload und verschiebt die Datei in den Ordner uploads/.
<?php /**
* PHP-Skript für Datei-Uploads * Standort: /var/www/html/index.php */
$upload_dir = 'uploads/'; $status_message = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file_upload'])) {
$file = $_FILES['file_upload'];
if ($file['error'] === UPLOAD_ERR_OK) {
$filename = basename($file['name']);
$target_path = $upload_dir . $filename;
if (move_uploaded_file($file['tmp_name'], $target_path)) {
$status_message = "Datei erfolgreich hochgeladen.";
} else {
$status_message = "Fehler: Datei konnte nicht gespeichert werden.";
}
} else {
$status_message = "Upload-Fehler. Code: " . $file['error'];
}
} ?>
<!DOCTYPE html> <html lang="de"> <head>
<meta charset="UTF-8"> <title>Upload</title>
</head> <body>
Datei-Upload
<?php if ($status_message): ?>
<?php echo $status_message; ?>
<?php endif; ?>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_upload" required>
<input type="submit" value="Hochladen">
</form>
</body> </html>
Sicherheitseinstellungen
Um die Ausführung von Skripten im Upload-Verzeichnis zu verhindern, wird dort eine .htaccess-Datei erstellt.
cd /var/www/html/uploads printf "php_flag engine off\nOptions -ExecCGI" > .htaccess