Friday, May 6, 2022

[SOLVED] Simple PHP/HTML upload page - no files are saving

Issue

I'm new to HTML/PHP and I'm trying to create a simple php file upload page. I have this as my HTML

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

I have this as my php:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

I've uploaded both of these to the correct folder in my host (000webhost), yet when I check in my /uploads folder nothing is there. I've granted all my files read write and execute permissions to try and debug it - I'll learn about security later.

Any help would be greatly appreciated!


Solution

You must add this snippet of code:

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);

after that:

echo "File is an image - " . $check["mime"] . ".";


Answered By - Dawlatzai Ghousi
Answer Checked By - Gilberto Lyons (WPSolving Admin)