Jump to content

Recommended Posts

I have a site that lets users uploads photos. I want to add on the user's username to the filename incase another user uploads the same filename.

$smallpics = './smallpics/'.$_FILES['file']['name'].'$user'; 

  $user is the username.  it doesn't work as it takes it as a new directory.

What can i do?

Thanks alot.

Link to comment
https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/
Share on other sites

i am sure adding the var user to the end off the upload will work

 

example ok.

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $user)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?> 

I've now got-

$smallpics = './smallpics/'.$user.$_FILES['file']['name'];

which puts the username in front of the filename which i'm happy with.

 

However when it moves the uploaded file from temporary server into the final destinatiomn it stiill keeps original name which i don't want. i got-

move_uploaded_file($user.$fieldname['tmp_name'], $uploadFilename);

  but it doesn't work.

i put

$fieldname = ($user.$_FILES['file']);

$user is garydt and the filename is gary.jpg.  i want it to become - garydtgary.jpg

When i echo $fieldname i get-

garydtArray

 

move_uploaded_file($fieldname['tmp_name'], $uploadFilename);

When i echo $uploadFilename i get -

./uploads/garydtg

 

what happens to the filename? it should be-

./uploads/garydtgary.jpg

 

I've been playing around with this a little and I think this may help you.

NOTE: I've pulled this out in a few minutes. I've test it and it seems to work on my server! I also don't use preg_match very often, but I'm sure some one can clean this up a bit.  ;)

 

<?php
$Save_Location = "./smallpics/";
$Image_Name = $_FILES['file']['name'];

preg_match("/([-_.a-z0-9]*)(.jpg|.gif|.png)/i", $Image_Name, $Matches);
$Save_Image_To = $Save_Location . $Matches[1] . "_" . $user . $Matches[2];

move_uploaded_file($_FILES['file']['tmp_name'], $Save_Image_To);
?>

 

The idea here is to isolate the file extension. Since you're uploading image we'll look for the common (*.jpg, *.gif and *.png) You can add more to. Once we find the file extension we simply add the user name after the filename then add the file extension.

 

Again, I am not the best with preg_match but this DOES WORK ON MY SERVER! But I know some one can touch this up.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.