31 Posts
Catastrophix
9 years ago
7
Topic

Dear SEBLOD family,

I'd like some general pointers on the best way to change the file name of an upload to incorporate some of the form values e.g.art_created_by and art_id.

Can anyone point me in the right direction?

Get a VIP membership
9 years ago
3
Level 1

Hello Catastrophix,

I would say that this change can only be done in the afterstore event.


So you can add a field Afterstore in you form, and add the good PHP code to archive what you want.

For that, in the variable $fields, you will get informations of each field of the form, so you can get the art_created_by and the art_id.

Then you will have to do 2 things :

  • Change the name of the file uploaded on the server
  • Change the path in database for this upload_file with the updated name.


Hope it will put you on the way.


Regards.

Lionel

31 Posts
Catastrophix
9 years ago
0
Level 2

It will indeed, thanks so much for pointing me in the right direction Lionel.

All of the best,

Stephen

548 Posts
joomleb
7 years ago
0
Level 2

Hi Lionel, 

don't finding any others suggestions, just for asking if, after two years, Is this still the only/simple way to rename a file when uploading (uploaded) ?

548 Posts
joomleb
6 years ago
0
Level 2

Hi Lionel,

as far as I understand, this is still the only solution on the way, right ?

Please,

1 - "add a field Afterstore in you form" - Do you mean this field, right ?

2 - "add the good PHP code to archive what you want. For that, in the variable $fields, you will get informations of each field of the form, so you can get the art_created_by and the art_id." - I cannot find a guide / tutorial onhow to use the Afterstore field, Please, Can anyone help me on it ? I want add a date field I created into _#cck_store_item_users table (padlock open),+ Name + Last Name.

Example: "20140131-John-Travolta.pdf"


Then, for all the old files (yet uploaded):

3 - "Change the name of the file uploaded on the server" - One by one, manually, right ?

4 - "Change the path in database for this upload_file with the updated name" - One by one, manually, right ? Please, where is saved the path exactly ?

Alternatively, for point 3 and 4, Can I order to all the users to enter on its profile and just click save (to run the afterstore plugin automatically) ?

4229 Posts
Kadministrator
7 years ago
1
Level 1

Don't think this has changed  in any way.

548 Posts
joomleb
6 years ago
0
Level 2

Hi Klas, 

Please, Can you confirm the points in my previous post ?

And, What about if the file name yet exist ? (Rare but not impossible) - Reference: Unique Alias automaticly

6 years ago
0
Level 1

Hi Joomleb,

1 - yes, in this pack ou have the plugin field "Afterstore".
You can add PHP code directly in the field (Textarea) or in a PHP file (relative path of the file in the field)

2 - When the code run, after the form have been submitted, you have access to 2 variables :

  • $fields => an array of each field in the form
    • $fields['art_created_by']...
    • You can access properties of a filed like that : $fields['art_created_by']->value, $fields['art_created_by']->text, ...
  •  $config => some informations of the submission
    • $config['pk'] => the id of the content created
    • $config['isNew'] => if it's a creation of content
    • ...


For your case, you must have a "Upload File" with a storage on the #__cck_store_item_users (Padlock open) for example named "my_file"
So in the afterstore you have informations like :

$table   =   $fields['my_file']->storage_table;
$field   =   $fields['my_file']->storage_field;
$id      =   $fields['art_id']->value;

$new_name    =   $date.'-'.$first_name.'-'.$last_name.'.pdf';

//
$query    =   'UPDATE '.$table.' SET '.$field.'="'.$new_name.'" WHERE id='.(int)$id;
JCckDatabase::execute( $query );


It's only an example, you must adapt to your case.

3 / 4 - if you have all informations in the same row for each file in the the database, you can write a script for doing that for you.

- get all rows which need to be modified.
- execute a foreach on the rows to update each row.


Regards.

Get a VIP membership