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

在我的WordPress网站中, 用户两次上传相同的图像文件并进行三次, 因此我的网站的磁盘空间超出了。

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


#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;
}

希望这对你有所帮助。

来源:

https://www.srcmini02.com/65425.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37294.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?