简体   繁体   English

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

[英]store uploaded file with user id name

I have a folder in my img folder! 我的img文件夹中有一个文件夹! users avatar img is supposed to go there! 用户头像img应该去那里! The issue is I want to save the pics with the user id name. 问题是我想用用户ID名称保存图片。 For example user with id 1 has a pic named 1.jpg so i should rename the file $id. 例如,id为1的用户有一个名为1.jpg的图片,因此我应该重命名文件$ id。 The problem is file extensions are removed! 问题是文件扩展名被删除! Look at the code below: 看下面的代码:

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

How should I change this line for my purpose? 我应该如何更改此行? I want this: If user with id 1 uploads avatar.png it stores in folder with the name 1.png (I will encode the id later). 我想要这样:如果ID为1的用户上载avatar.png,则它将存储在名称为1.png的文件夹中(稍后我将对ID进行编码)。

Also, imagine i already have a variable which have user id ready called $id. 另外,假设我已经有一个变量,该变量具有准备好的用户ID,称为$ id。

$path = $_FILES['file_upload']['name'];

$ext = pathinfo($path,PATHINFO_EXTENSION);

$ext is the extension, while "$userid.$ext" gives you UserID.extension $ ext是扩展名,而"$userid.$ext"为您提供UserID.extension

Do you thought about something like this? 您是否考虑过这样的事情?

$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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM