Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Uploading Files with PHP | File Handling and Uploads
PHP Core Concepts

bookUploading Files with PHP

When you allow users to upload files using PHP, the process begins with an HTML form containing an input of type file. The form must use the enctype="multipart/form-data" attribute to enable file uploads. When the user submits the form, the file is sent to the server as part of the HTTP request.

PHP automatically handles uploaded files by storing them in a temporary directory on the server. Information about each uploaded file is made available in the $_FILES superglobal array. This array contains details such as the original file name, the temporary file path, and any upload errors.

index.php

index.php

copy
12345678910111213141516171819202122232425262728293031323334
<?php // Handle the file upload when the form is submitted $message = ""; if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES["uploaded_file"])) { // Get the uploaded file information $fileTmpPath = $_FILES["uploaded_file"]["tmp_name"]; $fileName = $_FILES["uploaded_file"]["name"]; $destination = "uploads/" . basename($fileName); // Move the file from temporary directory to the uploads folder if (move_uploaded_file($fileTmpPath, $destination)) { $message = "File uploaded successfully to " . $destination; } else { $message = "Error uploading the file."; } } ?> <!DOCTYPE html> <html> <head> <title>Simple File Upload</title> </head> <body> <h2>Upload a File</h2> <?php if ($message): ?> <p><?php echo htmlspecialchars($message); ?></p> <?php endif; ?> <form action="index.php" method="post" enctype="multipart/form-data"> <label for="uploaded_file">Choose file:</label> <input type="file" name="uploaded_file" id="uploaded_file" required> <button type="submit">Upload</button> </form> </body> </html>

The uploaded file is not immediately saved to a permanent location. Instead, PHP places it in a temporary directory defined by the server configuration (commonly /tmp on Linux or a similar location on Windows). To keep the file, you must move it from the temporary directory to a permanent folder using a function like move_uploaded_file(). If you do not move the file, it will be deleted automatically when the script ends.

question mark

Where are uploaded files temporarily stored before being moved to a permanent location in PHP?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

How do I use `move_uploaded_file()` to save the uploaded file?

What information does the `$_FILES` array provide about the uploaded file?

Are there any security considerations when handling file uploads in PHP?

bookUploading Files with PHP

Swipe um das Menü anzuzeigen

When you allow users to upload files using PHP, the process begins with an HTML form containing an input of type file. The form must use the enctype="multipart/form-data" attribute to enable file uploads. When the user submits the form, the file is sent to the server as part of the HTTP request.

PHP automatically handles uploaded files by storing them in a temporary directory on the server. Information about each uploaded file is made available in the $_FILES superglobal array. This array contains details such as the original file name, the temporary file path, and any upload errors.

index.php

index.php

copy
12345678910111213141516171819202122232425262728293031323334
<?php // Handle the file upload when the form is submitted $message = ""; if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES["uploaded_file"])) { // Get the uploaded file information $fileTmpPath = $_FILES["uploaded_file"]["tmp_name"]; $fileName = $_FILES["uploaded_file"]["name"]; $destination = "uploads/" . basename($fileName); // Move the file from temporary directory to the uploads folder if (move_uploaded_file($fileTmpPath, $destination)) { $message = "File uploaded successfully to " . $destination; } else { $message = "Error uploading the file."; } } ?> <!DOCTYPE html> <html> <head> <title>Simple File Upload</title> </head> <body> <h2>Upload a File</h2> <?php if ($message): ?> <p><?php echo htmlspecialchars($message); ?></p> <?php endif; ?> <form action="index.php" method="post" enctype="multipart/form-data"> <label for="uploaded_file">Choose file:</label> <input type="file" name="uploaded_file" id="uploaded_file" required> <button type="submit">Upload</button> </form> </body> </html>

The uploaded file is not immediately saved to a permanent location. Instead, PHP places it in a temporary directory defined by the server configuration (commonly /tmp on Linux or a similar location on Windows). To keep the file, you must move it from the temporary directory to a permanent folder using a function like move_uploaded_file(). If you do not move the file, it will be deleted automatically when the script ends.

question mark

Where are uploaded files temporarily stored before being moved to a permanent location in PHP?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 3
some-alt