duanpa1980 2014-05-21 16:24
浏览 22
已采纳

在wordpress上传之前重命名图像[关闭]

I have a blog in wordpress where I upload multiples images per post, but It causing me and issue.

If the filename have whitespaces, special chars or something like that, It broke the upload. Also this issue is overwriting me the files with the same name. For example: First I upload a photo whose filename is "car.png". Then i upload other image with the same name, "car.png", It show me the first photo that I was uploaded.

I want to rename the files previuosly the upload with a date hash (year, month, day, hour, minute and second when It was uploaded).

How can I do this with a hook or a codex function?

Regards.

  • 写回答

1条回答 默认 最新

  • doulu1325 2014-05-21 16:28
    关注

    Here's a function I always add to my WP Themes Functions File:

    function sanitize_file_uploads( $file ){
        $file['name'] = sanitize_file_name($file['name']);
        $file['name'] = preg_replace("/[^a-zA-Z0-9\_\-\.]/", "", $file['name']);
        $file['name'] = strtolower($file['name']);
        add_filter('sanitize_file_name', 'remove_accents');
    
        return $file;
    }
    add_filter('wp_handle_upload_prefilter', 'sanitize_file_uploads');
    

    Trims is, remove special unaccepted characters, converts name to lowercase, and remove accents. Hopefully it works for you!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?