PHP upload + dropzoneJS : 只允许一些文件扩展名

PHP upload + dropzoneJS : allow only some files extensions

我在 PHP 做一个简单的网站,我正在使用 DropzoneJS 上传文件。
我试图只允许以下扩展:

pdf, png, jpg, jpeg, bmp and txt


有什么帮助吗?对不起英语不好
这是我的 upload.php 代码

$ds = DIRECTORY_SEPARATOR;
   $foldername = "../common/uploads";

    if (!empty($_FILES)) {
    $fileupload = basename( $_FILES['file']['name']);
    $fileType = $_FILES['file']['type'];
    $fileSize = $_FILES['file']['size'];

    $tempFile = $_FILES['file']['tmp_name'];
    $targetPath = dirname( __FILE__ ) . $ds. $foldername . $ds;
    $targetFile =  $targetPath. $fileupload;
    move_uploaded_file($tempFile,$targetFile);
    $upload_info = "Filetype : <b>".$fileType."</b> <br>File size : <b>".$fileSize."</b><br>File name : <b> ".$fileupload."</b>";
    add_to_log($_SESSION['username'], 'upload', $upload_info);
    }

干杯,MrZ.

首先,无论是dropzone发送文件还是文件上传输入都没有关系。它最终将在变量 $_FILES 中结束。即使受到dropzone的限制,你也应该保留验证服务器端。

所以您需要搜索的是

PHP upload+ dropzoneJS :allow only some files extensions

虽然我在做,但我是这样做的。

$valid_exts = array(// allowed extensions
                'gif',
                'jpeg',
                'jpg',
                'JPG',
                'png',
            );
$valid_types = array(
                'image/gif',
                'image/jpeg',
                'image/jpg',
                'image/pjpeg',
                'image/x-png',
                'image/png',
            );
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);

// make sure extension and type are allowed
    if (!(in_array($file['type'], $valid_types) && in_array($extension, $valid_exts))) {
            echo 'File type not allowed.';
            die();
    } else {
      // Do what ever you wish.
    }