mercredi 11 mars 2020

Why My switch statement is not working when it comes to php and uploading files to my computer

<?php 

    if(isset($_POST['submit'])){

        //$file= $_FILES['file'];

        //print_r($file);

        $fileName=$_FILES['file']['name'];

        $fileTemp=$FILES['file']['temp_name'];

        $fileType=$_FILES['file']['type'];

        $fileSize=$_FILES['file']['Size'];

        $fileError=$_FILES['file']['error'];

        $file_extention=explode(".",$fileName);

        $file_actEX=strtolower(end($file_extention));

    $allowed= array('jpg','jpeg','pdf','png');

// why can't my switch statement work

    switch(in_array($file_actEX,$allowed)){

        case $fileError !== 0;

       //this case shows if there is more than one error then it will not upload the file

        echo "Faild";

        break;

        case $fileSize<5000000;

// THIS SHOWS THAT IF THE FILE SIZE IS TOO BIG IT WON'T UPLOAD THE FILE

        $fileNameNew=uniqid("",true).".".$file_actEX;

// THIS GIVES THE FILE A UNIQUE ID WHEN SOMEONE TRIES TO UPLOAD A FILE WITH SAME NAME

        $fileDestination= 'uploads/'.$fileNameNew;

// THIS SENDS THE FILE TO THE LOCATION I WANT ON MY COMPUTER WHICH IS MY UPLOADS FOLDER

        move_uploaded_file($fileTemp,$fileDestination);

// THIS SENDS THE FILE FROM THE TEMPORARY LOCATION THE ACTUAL LOCATION

        header("Location:embed.php?Uploadaccepted");

// THIS SEND THE USER TO THE SAME FILE EMBED.PHP WHEN HE CLICKS THE UPLOAD BUTTON.

        break;

        case $fileSize> 5000000;

//This condition says if the file is to big we won't upload the file

        echo "File Too Big";

        break;

        default:

        echo "File Not Allowed";








    }





}



<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>
    <form action="upload2.php" method="POST" enctype="multipart/form-data">

        <input type="file" name="file" value="Choose File">

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

    </form>
</body>

</html>

Aucun commentaire:

Enregistrer un commentaire