Rename uploaded file (php)

佐手、 提交于 2019-11-29 18:19:50

Rename the file as below

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$target_file = $target_dir . $id . '.' . $imageFileType;

And then

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);

You set the target file to be the name of the file. You should be setting a different name.

Try this:

$target_dir = "files/$id/";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$target_file = "{$target_dir}{$id}.{$imageFileType};

Thanks guy, it all helped.

My solution was to make a variable with the extension, and then use that through out the file.

//Check file extension
$path = $_FILES["fileToUpload"]["name"];
$ext = pathinfo($path, PATHINFO_EXTENSION);

and then

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "files/$id/$id.$ext"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!