【问题标题】:PHP Uploads -- Disallow certain extensions bad?PHP 上传 - 不允许某些扩展不好?
【发布时间】:2025-12-23 09:00:12
【问题描述】:

我正在处理一个最多允许 300MB 的上传表单。

我们的客户不想规范上传哪些文件,因为他们的客户可能会发送一个大的图像文件,例如 png、tiff、psd 等。

“禁止”列表会比允许更好吗?它将其移动到上传文件夹。我让上传文件夹全部拒绝并隐藏 htaccess 中的索引,同时添加他们的 IP 以阻止,然后显示 404 页面。如果他们也尝试直接访问文件,这也有效。

我不希望人们上传 .php、.php5、.asp、.exe 等

是否有我可以找到的文件列表来禁止此类文件,或者只是从头开始编写它们?

抱歉跑题了。

谢谢

【问题讨论】:

  • 您最好选择白名单而不是黑名单。只允许您特别需要的类型的文件,在这种情况下看起来只是某些图像类型。
  • 他们还获取 docs、docx、pdf 和其他随机文件。我不想禁止其他与我没有关联的文件,而且他们不知道他们之前获得的所有文件。

标签: php file-upload


【解决方案1】:

出于安全原因,最好使用允许列表(白名单)而不是禁止列表(黑名单)。这是一个非常全面的文件数组及其允许的 mimetypes,取自 wordpress:

array(
    // Image formats
    'jpg|jpeg|jpe' => 'image/jpeg',
    'gif' => 'image/gif',
    'png' => 'image/png',
    'bmp' => 'image/bmp',
    'tif|tiff' => 'image/tiff',
    'ico' => 'image/x-icon',
    // Video formats
    'asf|asx|wax|wmv|wmx' => 'video/asf',
    'avi' => 'video/avi',
    'divx' => 'video/divx',
    'flv' => 'video/x-flv',
    'mov|qt' => 'video/quicktime',
    'mpeg|mpg|mpe' => 'video/mpeg',
    'mp4|m4v' => 'video/mp4',
    'ogv' => 'video/ogg',
    'mkv' => 'video/x-matroska',
    // Text formats
    'txt|asc|c|cc|h' => 'text/plain',
    'csv' => 'text/csv',
    'tsv' => 'text/tab-separated-values',
    'ics' => 'text/calendar',
    'rtx' => 'text/richtext',
    'css' => 'text/css',
    'htm|html' => 'text/html',
    // Audio formats
    'mp3|m4a|m4b' => 'audio/mpeg',
    'ra|ram' => 'audio/x-realaudio',
    'wav' => 'audio/wav',
    'ogg|oga' => 'audio/ogg',
    'mid|midi' => 'audio/midi',
    'wma' => 'audio/wma',
    'mka' => 'audio/x-matroska',
    // Misc application formats
    'rtf' => 'application/rtf',
    'js' => 'application/javascript',
    'pdf' => 'application/pdf',
    'swf' => 'application/x-shockwave-flash',
    'class' => 'application/java',
    'tar' => 'application/x-tar',
    'zip' => 'application/zip',
    'gz|gzip' => 'application/x-gzip',
    'rar' => 'application/rar',
    '7z' => 'application/x-7z-compressed',
    // MS Office formats
    'doc' => 'application/msword',
    'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
    'wri' => 'application/vnd.ms-write',
    'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
    'mdb' => 'application/vnd.ms-access',
    'mpp' => 'application/vnd.ms-project',
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
    'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
    'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
    'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
    'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
    'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
    'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
    'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
    'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
    'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
    'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
    'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
    'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
    'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
    'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
    'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
    // OpenOffice formats
    'odt' => 'application/vnd.oasis.opendocument.text',
    'odp' => 'application/vnd.oasis.opendocument.presentation',
    'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    'odg' => 'application/vnd.oasis.opendocument.graphics',
    'odc' => 'application/vnd.oasis.opendocument.chart',
    'odb' => 'application/vnd.oasis.opendocument.database',
    'odf' => 'application/vnd.oasis.opendocument.formula',
    // WordPerfect formats
    'wp|wpd' => 'application/wordperfect',
    );

如果缺少任何内容,您可以添加它,但这应该涵盖他们需要上传的几乎所有文件,同时不允许任何潜在的恶意文件。

【讨论】:

    【解决方案2】:

    获得扩展程序后,您可以执行以下操作:

    $not_allowed = array('php', 'php5', 'exe');
    
    if(in_array($extension, $not_allowed)){
        echo 'File not allowed';
    }else{
        echo 'File allowed';
    }
    

    或者,相反:

    $allowed = array('doc', 'pdf', 'docx');
    
    if(in_array($extension, $allowed)){
        echo 'File allowed';
    }else{
        echo 'File not allowed';
    }
    

    【讨论】:

    • 我知道该怎么做,我在问“我可以找到一个文件列表来禁止这样的文件,还是从头开始编写它们?”
    • 哦,对不起。我发现this 的帖子可能会有所帮助。如上所述,您最好只列出您允许的内容并从那里开始。