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 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存