Forum Moderators: coopster

Message Too Old, No Replies

changing the name of a file upload

         

knotworking

5:50 pm on Apr 20, 2005 (gmt 0)

10+ Year Member



Hi everybody,

I'm stuck on this one. I upload a file via my form, it is then attached to an email that is automatically generated from the form (attaching using a mime.inc document). So, it's attaching from the temporary php upload directory (I think).

What I'm trying to do is assign the file upload a new name as it is uploaded. I'd like to rename it to the form values $first_name $last_name (so it would be something like "John Doe.doc").

1. Is it even possible to rename the upload using these values (I'm fairly sure the answer is yes)

2. Does this renaming effect the file extension? (my users upload various types of files)

3. My upload field name is $file_upload. This variable is referenced throughout. Would I have to change this reference or, can I just assign the new name to the variable.

I appreciate anyone's help, this one is stumping me.

ergophobe

8:07 pm on Apr 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to use move_uploaded_file() [php.net]

Follow that link and that should get you what you want on questions #1 and #2.

As for the extension, you'll want to find out what it is and use the same extension. The original file name is preserved in

$_FILES['userfile']['name']

where 'userfile' is the name you give to the $_FILES array on you upload form. You can get the extension with something like this

$name_parts = explode('.', $_FILES['userfile']['name']);
$extension_index = count($name_parts) - 1;
if ($extension_index)
{
$extension = $name_parts[$extension_index];
}

see [us2.php.net...] for more

Note that in this case you can't use pathinfo() [php.net] but it's a good thing to remember for getting file extensions.