使用用户id名称存储上载的文件


store uploaded file with user id name

我的img文件夹中有一个文件夹!用户头像img应该去那里!问题是我想用用户名保存图片。例如,id为1的用户有一个名为1.jpg的图片,所以我应该重命名文件$id。问题是文件扩展名被删除了!看看下面的代码:

$target_file = basename(time().$_FILES['file_upload']['name']);

我该如何更改此行以达到我的目的?我想要这个:如果id为1的用户上传了avatar.png,它会存储在名为1.png的文件夹中(我稍后会对id进行编码)。

此外,假设我已经有了一个变量,该变量的用户id已经就绪,名为$id。

$path = $_FILES['file_upload']['name'];
$ext = pathinfo($path,PATHINFO_EXTENSION);

$ext是扩展,而"$userid.$ext"为您提供UserID.extension

你想过这样的事情吗?

$userID = 1;
$path = "/absolute/path/to/your/upload/folder/";
//explode the filename so we get the extension
$fileData = explode(".", $_FILES['file_upload']['name']);
//considering the filename "avatar.png" $target_file will contain "/absolute/path/to/your/upload/folder/1.png"
$target_file = $path . $userID . "." . $fileData[1]);