0v3k Shi3ld3r 0v3k Shi3ld3r - 4 months ago 8
SQL Question

Rename image? To users name , Upon Uploading

I would like to know how to rename an avatar before/upon its uploaded , I have searched around and also tried a few changes to my code , I already have managed to get the photo to

upload
change the name but the
photo
is unknown file type . Here is my
code
below

$move_file = move_uploaded_file($file['tmp_name'], $upload_image_to_folder.$new_file_name);


$new_file_name
in my case is the file name EG: image.jpg will echo from this . How can I rename this to the users name

I would like to rename the image to the username that is uploading it

EG
$new_file_name = $username
<-- users name

Answer

Possibly you are missing to add a file extension to the filename...

You can try this:

$filetype = pathinfo(basename($_FILES["fileToUpload"]["name"]),PATHINFO_EXTENSION);
$target_file = $target_dir . $username . "." . $filetype;

Now this will convert the name to the $username and append it's original file extension... Next you can use:

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

P.S - This code is with reference for your situation that file is Sent to PHP via a form having variable name fileToUpload in HTML code... You might Need to Change accordingly, because I don't know your whole code...

Hope this helps... :)