0v3k Shi3ld3r 0v3k Shi3ld3r - 1 year ago 71
MySQL Question

Rename image? To users name , Upon uplaoding

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

change the name but the
is unknown file type . Here is my

$move_file = move_uploaded_file($file['tmp_name'], $upload_image_to_folder.$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

$new_file_name = $username
<-- users name

Answer Source

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... :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download