简体   繁体   中英

store uploaded file with user id name

I have a folder in my img folder! users avatar img is supposed to go there! The issue is I want to save the pics with the user id name. For example user with id 1 has a pic named 1.jpg so i should rename the file $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).

Also, imagine i already have a variable which have user id ready called $id.

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

$ext = pathinfo($path,PATHINFO_EXTENSION);

$ext is the extension, while "$userid.$ext" gives you 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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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