【问题标题】:how can I disable php file upload in wordpress如何在wordpress中禁用php文件上传
【发布时间】:2016-09-19 15:56:53
【问题描述】:

我正在尝试为前端的 wordpress 用户上传头像图像。我正在使用此代码

$files = $_FILES['post_files'];

foreach($files as $file){

  if(is_array($file)){

    $uploaded_file_type = $file['type'];
    $allowed_file_types = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif');
    if(!in_array($uploaded_file_type, $allowed_file_types)) {
      $errors['image_empty'] = __( 'this image is not valid', 'themename' );
    } 

  }

}

不允许上传 php 文件,但如果他们将 php 文件扩展名更改为 png 或 jpeg,他们可以将 php 文件上传到我的服务器。我尝试使用getimagesize(),但我不能,我是 php 的新手。或者有没有其他解决方案?

感谢回答

【问题讨论】:

  • 为什么需要检查这个?如果他们将 PHP 文件作为图像上传,最糟糕的情况是图像不会显示 - 您的服务器不会在其中运行 PHP(除非配置真的很糟糕:))。
  • 我正在使用dreamhost,如果我可以上传shell.php,我可以访问所有文件并可以编辑
  • 如果它没有 .php 扩展名则不会...

标签: php wordpress image upload


【解决方案1】:

如果您使用 Wordpress 图片上传器(媒体上传器 vs.),请使用:

禁用任何文件格式:

add_filter('upload_mimes','remove_mime_types');
function remove_mime_types($mimes){
  unset( $mimes['mp4'] );
}

启用任何文件格式:

add_filter('upload_mimes','add_custom_mime_types');
function add_custom_mime_types($mimes){
   return array_merge($mimes,array (
     'ac3' => 'audio/ac3',
     'mpa' => 'audio/MPA',
     'flv' => 'video/x-flv',
     'svg' => 'image/svg+xml'
   ));
}

欲了解更多信息,请访问 paulund 的文章:https://paulund.co.uk/change-wordpress-upload-mime-types

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2017-10-03
    • 2019-03-14
    相关资源
    最近更新 更多