0

I'm trying to make a File Management System, the upload form and the php action is the same file, I've followed everything correctly and yet I get the same error:

Warning: finfo_file(): Empty filename or path in /Applications/XAMPP/xamppfiles/htdocs/Year1/Period2/Project/Assignments/WS_FileManagement/Upload.php on line 10
Resource id #2Allowed types: .png, .jpeg, .jpg, .gif
upload

I did upload the correct type.

I am trying to upload a random image from the internet (put on my desktop) but it doesn't work, please help.

Here's the code:

<?php


    if(isset($_GET['submit']))
    {
        if($_FILES['imagefile']['size'] < 3000000)
        {
            $accepted = ["image/png", "image/jpeg", "image/jpg", "image/gif"];
            $fileinfo = finfo_open(FILEINFO_MIME_TYPE);
            $fileType = finfo_file($fileinfo, $_FILES["imagefile"]["tmp_name"]);
            echo $fileinfo;
            echo $fileType;

            if(in_array($fileType, $accepted))
            {
                if($_FILES['imagefile']['error']>0)
                {
                    echo "<br>Error: ". $_FILES['imagefile']['error']."<br>";
                }
                else
                {
                    if(file_exists("images/".$_FILES['imagefile']['name']))
                    {
                        echo $_FILES['imagefile']['name']." already exists";
                    }
                    else
                    {
                        if(move_uploaded_file($_FILES['fimagefileile']['tmp_name'], "images/".$_FILES['file']['name']))
                        {
                            echo "Success!";
                        }
                        else
                        {
                            echo "Something went wrong!";
                        }
                    }
                }
            }
            else
            {
                echo "Allowed types: .png, .jpeg, .jpg, .gif";
            }
        }
        else
        {
            echo "Please upload a file under 3MB!";
        }
    }

?>

/images is in the same folder as the file

EDIT:

$fileinfo returns false! why?

DaviHlav
  • 17
  • 3
  • Check your max upload size for PHP: https://stackoverflow.com/a/30359278/231316 – Chris Haas Nov 24 '21 at 17:52
  • 3
    RTM: https://www.php.net/manual/en/features.file-upload.post-method.php (and 1000 other tutorials) . You can't upload a file with GET and you're missing the enctype on the form tag. – ADyson Nov 24 '21 at 17:53
  • I voted to close this question because the code sample is too long. Please [edit] your post to pare your code down to a [minimal, complete, readable, and reproducible example](/help/minimal-reproducible-example). – Stephen Ostermiller Nov 24 '21 at 22:40

0 Answers0