doudi4137 2016-11-15 10:51
浏览 46

在PHP中为上传的文件添加随机数

I have my upload php code where my intent is , obtained file from $_files, add a random number between 0 and 9999 to the name of image like this:

image sent : image.jpg
before saving : image321.jpg

the image is saved in my upload folder but the filename are like "php2983204tmp"

if ($file !== null) {
    $rand = rand(0000,9999);
    $path = "some_path";

    $file_name = $file->getClientOriginalName(); // file
    $extension = $file->getClientOriginalExtension(); // jpg

    $file->move($path, $file_name.$rand.$extension);

    $response = "File loaded successfully: " . $file_name.$extension;
    $response .= '<br>size: ' . filesize($path . '/' . $file->getClientOriginalName()) / 1024 . ' kb';

    return new Response($response);

any ideas to fix?

  • 写回答

3条回答 默认 最新

  • donglong9745 2016-11-15 10:55
    关注

    The filename in your example is php and your extension is tmp. None of them have the . that you are missing.

    You need to add the dot . as a string after the $file_name and $rand, before the $extension like this:

    $file->move($path, $file_name.$rand. "." .$extension);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)