duanbixia7738 2014-03-23 12:45
浏览 52
已采纳

在上传之前完全重命名照片 - php

I'd like to totally rename a file before it uploads. Other posts I've found on here seem to append something to the beginning or the end of the file name to make it unique, but I just want to completely wipe the name and rename it.

Input: anyoldfilename.jpg output: thisnameexactly.jpg

The following function I'm working with uploads but doesn't rename the file. The name should be $newfilename, which I've created in the function:

$target = "../files/photos/"
$target = $target . basename( $_FILES['photo']['name']);

//Find file extension of photo
$newfilename = "thisnameexactly";
$ext = end(explode('.', $_FILES['photo']['name']));
$newfilename = $newfilename . "." . $ext;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
   echo "image successfully uploaded";
}
else {
   echo "Sorry, there was a problem uploading your file.";
}

How can I modify this so that it renames the file entirely?

  • 写回答

1条回答 默认 最新

  • douruoshen1449 2014-03-23 12:49
    关注
    $target = $target . basename( $_FILES['photo']['name']); 
    

    you have on the top, move it as

    $target = "../files/photos/"
    
    //Find file extension of photo
    $newfilename = "thisnameexactly";
    $ext = end(explode('.', $_FILES['photo']['name']));
    $newfilename = $newfilename . "." . $ext;
    
    $target = $target . $newfilename;
    
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
       echo "image successfully uploaded";
    }
    else {
       echo "Sorry, there was a problem uploading your file.";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部