garydt Posted July 2, 2007 Share Posted July 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/ Share on other sites More sharing options...
Harley1979 Posted July 2, 2007 Share Posted July 2, 2007 Try this... // image path $smallpics = "../smallpics/"; // image name $user = "user name"; // move temporary file to desired path and rename move_uploaded_files($_FILES['file']['name'], $smallpics.$user); Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-287900 Share on other sites More sharing options...
redarrow Posted July 2, 2007 Share Posted July 2, 2007 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-287902 Share on other sites More sharing options...
garydt Posted July 2, 2007 Author Share Posted July 2, 2007 Thanks It just added the username to the end of the filename, eg pic1.jpgtim Iff the uploaded filename is 'pic1.jpg' and the username is 'tim32' then i want the filename to become 'pic1tim32.jpg' Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-287928 Share on other sites More sharing options...
garydt Posted July 2, 2007 Author Share Posted July 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-287989 Share on other sites More sharing options...
garydt Posted July 2, 2007 Author Share Posted July 2, 2007 Can someone help? Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-288084 Share on other sites More sharing options...
garydt Posted July 2, 2007 Author Share Posted July 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-288127 Share on other sites More sharing options...
Salis Posted July 2, 2007 Share Posted July 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-288160 Share on other sites More sharing options...
corillo181 Posted July 2, 2007 Share Posted July 2, 2007 i think a easier answer should be given.. after you done with the picture and you are ready to save it.. if you are using your username as a session variable just add the username $_SESSION['username'].$filenameblahblahblah Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-288162 Share on other sites More sharing options...
garydt Posted July 3, 2007 Author Share Posted July 3, 2007 I'm still having trouble. i've tried- move_uploaded_file($fieldname['tmp_name'].$user, $uploadFilename); and i get - file.jpggarydt $user is garydt how do i get $user as part of filename before it is saved? Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-288843 Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 www.php.net/basename <?php $new_name = basename($fieldname['tmp_name'], '.jpg'); $new_name = $new_name . $user . ".jpg"; move_uploaded_file($new_name, $uploadFilename); Something like that would work. Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-288848 Share on other sites More sharing options...
Zane Posted July 3, 2007 Share Posted July 3, 2007 $theFile = pathinfo($_FILES['userfile']['name']); $new_name = basename($theFile['basename'], $theFile['extension']); $new_name .= $user . $theFile['extension']; move_uploaded_file($_FILES["magfile"]["tmp_name"], $new_name); Quote Link to comment https://forums.phpfreaks.com/topic/58063-altering-name-of-uploaded-file/#findComment-288871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.