How to show result after uploaded file in php?

Member

by daisha , in category: PHP General , 4 months ago

How to show result after uploaded file in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 4 months ago

@daisha 

To show the result after uploading a file in PHP, you can follow these steps:

  1. Create the HTML form to upload the file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<!DOCTYPE html>
<html>
<head>
    <title>File Upload</title>
</head>
<body>
    <form action="upload.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value="Upload">
    </form>
</body>
</html>


  1. Create the PHP script (upload.php) to handle the file upload and show the result:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
if ($_FILES["file"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"];
} else {
    // Uploading process
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);

    // Show success message
    echo "File uploaded successfully!";
}
?>


  1. Create a directory named "uploads" in the same directory as the PHP script to store the uploaded file.


When a file is selected and the form is submitted, it will be sent to upload.php using the POST method. In the PHP script, you can check if there were any errors during the file upload using $_FILES["file"]["error"]. If there's an error, you can display the corresponding message. If the upload was successful, move the uploaded file to the desired location (in this example, "uploads" directory) using move_uploaded_file() function and display a success message.