【问题标题】:PHP upload + dropzoneJS : allow only some files extensionsPHP 上传 + dropzoneJS :只允许某些文件扩展名
【发布时间】:2015-05-04 16:51:40
【问题描述】:

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

pdf、png、jpg、jpeg、bmp 和 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);
    }

干杯,Z先生。

【问题讨论】:

    标签: php upload dropzone.js


    【解决方案1】:

    首先,无论是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.
        }
    

    【讨论】:

    • 感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 2021-03-09
    • 1970-01-01
    • 2019-06-11
    • 2011-01-29
    • 2021-05-22
    相关资源
    最近更新 更多