phpimage

Cannot upload file to folder from form


admin.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin</title>
</head>
<body>
    <form action="upload.php" method="POST" enctype="multipart/form-data">
        Image: <input type="file" name="image">
        <br>
        Sale or Rent: <input type="text" name="isForSale">
        <br>
        Price: <input type="text" name="price">
        <br>
        Location: <input type="text" name="location">
        <br>
        Area: <input type="text" name="area">
        <br>
        Bedrooms: <input type="text" name="bedrooms">
        <br>
        <button type="submit">Upload</button>
    </form>
</body>
</html>

upload.php:

<?php
    if(isset($_POST['submit'])) {
        $file = $_FILES['image'];

        $fileName = $_FILES['image']['name'];
        $fileTmpName = $_FILES['image']['tmp_name'];
        $fileSize = $_FILES['image']['size'];
        $fileError = $_FILES['image']['error'];
        $fileType = $_FILES['image']['type'];

        $fileExt = explode('.',$fileName);
        $fileActExt = strtolower(end($fileExt));

        $allow = array('jpg','jpeg','png');

        if(in_array($fileActExt,$allow)) {
            if($fileError === 0) {
                $fileNewName = uniqid('', true).".".$fileActExt;
                $fileDestination = 'FamilyRealEstate/FamilyRealEstateImages/uploads'.$fileNewName;
                move_uploaded_file($fileTmpName, $fileDestination);
                header("Location: admin.php?uploadsuccess");
            } else {
                echo "Error uploading your file";
            }
        } else {
            echo "You cannot upload files of this type.(Only jpg, jpeg, png)";
        }
    }
?>

I'm trying to upload an image on the web and have it saved in a folder. This seems to not be working. It just goes to a blank page (upload.php). It might be because of the permissions of the folder which I'm uploading the images into, however I am using Ubuntu and am very new to it so I don't really know how to fix it.


Solution

  • Your submit button should contain the name attribute with the submit value:

    <button type="submit" name="submit">Upload</button>