Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.9 kB
7
Indexable
Never
<!DOCTYPE html>
<html>

<body>

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


</body>

</html>
<?php

function moveFile($file, $target)
{
    return move_uploaded_file($file, $target);
}

function makeDir($dir): bool
{

    if (is_dir($dir))
        return false;
    return mkdir($dir);
}
function fileExists($file)
{
    return file_exists($file);
}
function imageValidate($type)
{
    $types = ['jpg', 'png', 'jpeg', 'gif'];
    return in_array($type, $types);
}
function textValidate($type)
{
    $types = ['txt', 'docx'];
    return in_array($type, $types);
}
function fileSizeValidate($fileSize, $fileSizeCheck)
{
    return  $fileSize < $fileSizeCheck;
}



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



    $dir = __DIR__ . "\\";
    $targetDirText = 'text';
    $targetDirImage = 'image';
    makeDir($targetDirText);
    makeDir($targetDirImage);
    $file = $_FILES['fileToUpload'];
    $fileName = $file['name'];
    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
    $targetFile = $target_dir . $_POST['file_name'] . "\.$extension";
    $fileType = strtolower($extension);

    if (fileSizeValidate($file['size'], 1024 ** 2) and imageValidate($fileType)) {

        if (moveFile($file['tmp_name'], $targetDirImage . '/' . $fileName)) {
            echo 'upload image ';
           
        } else {
            echo 'upload filad';
          
        }
    } elseif (fileSizeValidate($file['size'] / 1024, 512) and imageValidate($fileType)) {
        if (moveFile($file['tmp_name'], $targetDirText.'/'.$fileName))
            echo 'upload image ';
        else
            echo 'upload filad';
    }
}




?>