0

when I try to upload a file to my website I got this [error message in php][1] of Undefined index and "Cannot upload this type of file" even though I try to upload an adequate format (jpg, pdf) as per my code below

PHP code

<?php
if (isset($_POST['submit'])) {
    $file = $_FILES ['file'];
    $fileName = $_FILES ['file'] ['name'];
    $fileTmpName = $_FILES ['file'] ['tmp_name'];
    $fileSize = $_FILES ['file'] ['size'];
    $fileError = $_FILES ['file'] ['error'];
    $fileType = $_FILES ['file'] ['type'];
    $fileExt = explode ('.',$fileName);
    $fileActualExt = strtolower (end($fileExt));
    $allowed = array ('jpg', 'jpeg', 'png', 'pdf', 'doc');
    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 10000000) {
                $fileNameNew = uniqid ('', true).".".$fileActualExt;
                $fileDestination = 'upload/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                header ("Location : Contact.html?uploadedsuccessfully");
            } else {
                echo "Your file is too big!";
            }  
        } else {
            echo "There was an error uploading your file!";
        } 
    } else {
        echo "You cannot upload files of this type!";
    }
}
    

HTML code/my form

<section class= "box3">
        <h4>Become a contributor</h4>
        <div class="lines"></div>
        <form class="contact-form" action="uploads.php" method= "POST" enctype= "multipart/form-data"> 
            <input type= "file" name="file" class= "contact-form-text" >
            <button type="submit" class="contact-form-btn" name="submit">Upload</button>
        </form>
    </section

Blockquote

P.S : I am testing my PHP code locally [1]: https://i.stack.imgur.com/sKylW.jpg

Hajar00
  • 15
  • 5

0 Answers0