PHP上传——不允许某些扩展坏?

PHP上传——不允许某些扩展坏?,php,file-upload,Php,File Upload,我有一个上传表单,我正在工作,将允许高达300MB 我们的客户不想管理上传的文件,因为他们的客户可能会发送一个大的图像文件,如png、tiff、psd等 “不允许”列表是否比“允许”列表更有效?它会将其移动到上载文件夹。我有上传文件夹deny all,隐藏htaccess中的索引,并将他们的IP添加到block中,然后显示404页面。如果他们试图直接访问一个文件,也可以这样做 我不希望人们上传.php、.php5、.asp、.exe等 是否有一个文件列表,我可以找到不允许这样的文件,或只是从头开

我有一个上传表单,我正在工作,将允许高达300MB

我们的客户不想管理上传的文件,因为他们的客户可能会发送一个大的图像文件,如png、tiff、psd等

“不允许”列表是否比“允许”列表更有效?它会将其移动到上载文件夹。我有上传文件夹deny all,隐藏htaccess中的索引,并将他们的IP添加到block中,然后显示404页面。如果他们试图直接访问一个文件,也可以这样做

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

是否有一个文件列表,我可以找到不允许这样的文件,或只是从头开始写它们

对不起,我离题了


谢谢

获得分机后,您可以执行以下操作:

$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';
}

出于安全原因,最好使用允许列表(白名单)而不是禁止列表(黑名单)。下面是一个相当全面的文件数组及其允许的mimetype,摘自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',
    );

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

您最好不要使用白名单而不是黑名单。只允许特定类型的文件,在本例中,这些文件看起来就像某些图像类型。它们还可以获取文档、docx、pdf和其他随机文件。我不想禁止与我无关的其他文件,他们也不知道以前收到的所有文件。我知道如何做到这一点,我在问“是否有一个文件列表,我可以找到它来禁止这样的文件,或者从头开始写?”哦,很抱歉。我找到了可能有用的帖子。就像上面提到的,您最好只列出您允许的内容并从中开始。