rename the uploaded file
DesignWork - August 10, 2007 - 11:07
| Project: | ImageField |
| Version: | 6.x-3.0 |
| Component: | User interface |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | won't fix |
Jump to:
Description
Hi Mantainers,
I would like to ask a question refering the imagefield (5x.1.1).
Is it possible to rename the uploaded file?
At the moment imagefield just takes the given filename and uses it. The problem is when you download the images to your desktop (drag and drop) the image has only the file name. I would like that it has the name of the user who posted the image and maybe the name of the website. like: misterx-mysite.jpg. This would very good for people researching the web they allways know where the image is from.
Maybe we could youse some code from autonode title and use the same tokens.
Thanks for replying
Dirk
PS. I can help coding it. Just wanted to change some ideas first.

#1
I would like to have this feature as well. It makes the filesystem much more organised. Any chance that this could be done? If there is no development time, I'd be happy to implement it, I just don't like patching unmaintained code by myself.
#2
(fixed typo)
I think this would be a great idea as well; particularly for those with galleries with heavy loads. An option to have uploaded files renamed, using tokens perhaps, thus minimizing the risk of overwriting a similarly-named image.
#3
Nope not implemented. If you write it, do it for DRUPAL-5--2 or HEAD.
#4
Is this being written for 2? This would be fantastic.
#5
Hey guys i am working on the same thing
hope will sort it out in day or two
#6
To rename file at upload time i use following code :
<?php
function _imagefield_widget_prepare_form_values(&$node, $field, &$items) {
$fieldname = $field['field_name'];
// clean up the session if we weren't posted.
if (!count($_POST)) {
imagefield_clear_session();
}
// Attach new files
if ($file = file_check_upload($fieldname . '_upload')) {
$file = (array)$file;
//**** NEW CODE : replace filename with new generated ************************
// allow tokenized paths.
if (function_exists('token_replace')) {
global $user;
$widget_image_path = token_replace($field['widget']['image_path'],'user', $user);
}
else {
$widget_image_path = $field['widget']['image_path'];
}
$filepath = file_create_path($widget_image_path); // directory where file located
$file_ext = ereg_replace( "^.*(\\.[^.]*)$", "\\1", $file['filename']);
do {// generates new name
$rndname = mt_rand().$file_ext;
}while (file_check_location($rndname,$filepath));
$file['filename'] = $rndname;
//*************************************************************
//...........
?>
#7
nope.. not taking that patch... My stance is rename files before uploading them...
#8
Is there any ideas how to implement this functionality without patch? It would be nice to see... Thanx.