如何防止用户两次上传同一个文件WordPress

问题描述 投票:-1回答:1

在我的WordPress网站上,用户上传相同的图像文件两次并且三次,所以我的网站的磁盘空间超过了。

是否有任何选项可以阻止用户一次又一次地上传相同的图像文件。

php wordpress wordpress-theming
1个回答
1
投票

我遇到过同样的问题。如果已经存在替换图像的过滤器钩子,则不添加任何验证,而是添加任何验证。将此代码添加到functions.php文件中

add_filter( 'sanitize_file_name', 'replace_image_if_same_exists', 10, 1 );

function replace_image_if_same_exists( $name ) 
{
  $args = array(
    'numberposts'   => -1,
    'post_type'     => 'attachment',
    'meta_query' => array(
            array( 
                'key' => '_wp_attached_file',
                'value' => $name,
                'compare' => 'LIKE'
            )
        )
  );
  $attachments_to_remove = get_posts( $args );

  foreach( $attachments_to_remove as $attach )
    wp_delete_attachment( $attach->ID, true );

 return $name;
}

希望这对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.