dpo15099 2019-06-06 21:20
浏览 125
已采纳

使用自定义文件名在WordPress上传文件

I'm currently developing a plugin for my page. The goal is to upload a custom profile image from a user page. Now I want to store each image under a pre-defined name. Sadly I've no plan how I can set a custom name for a file during the file upload with the WordPress function wp_upload_bits():

$filename =  $_FILES['file']['name'];

$uploaded_bits = wp_upload_bits(
    $filename,
    null,
    file_get_contents( $_FILES['file']['tmp_name'] )
);

Currently when I upload for example an image with the name yolo.png, the name stays yolo.png.

Now I want to define the new name this way:

$filename = 'profile-image-' . get_current_user_id();

But this can't work because the $_FILES['file']['name'] is the temporarily saved file on my server and this is yolo.png. So do you have any idea how I can reach my goal?

  • 写回答

2条回答 默认 最新

  • doutinghou6980 2019-06-06 22:00
    关注

    Today I'm not that smart. I need to pass the file type, otherwise the upload can't work...:

    $filename = 'profile-image-' . get_current_user_id() . '.' . substr( strrchr( $_FILES['file']['name'], '.' ), 1 );
    

    With this part of code I just get the file extension:

    '.' . substr( strrchr( $_FILES['file']['name'], '.' ), 1 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料