php上传文件时重命名,PHP - 上传并覆盖文件(或上传并重命名)?

标准表单就足以上传,只需记住在表单中包含mime即可。然后你可以使用$ _FILES ['']来引用该文件。

然后你可以检查提供的文件名并查看它是否存在于文件系统中使用file_exists()检查文件名或者如果你不需要保留旧文件,你可以使用perofrm文件移动并覆盖旧文件一个与临时目录中的新

// this assumes that the upload form calls the form file field "myupload"

$name = $_FILES['myupload']['name'];

$type = $_FILES['myupload']['type'];

$size = $_FILES['myupload']['size'];

$tmp = $_FILES['myupload']['tmp_name'];

$error = $_FILES['myupload']['error'];

$savepath = '/yourserverpath/';

$filelocation = $svaepath.$name;

// This won't upload if there was an error or if the file exists, hence the check

if (!file_exists($filelocation) && $error == 0) {

// echo "The file $filename exists";

// This will overwrite even if the file exists

move_uploaded_file($tmp, $filelocation);

}

// OR just leave out the "file_exists()" and check for the error,

// an if statement either way

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值