Forums

Rename an uploaded file?

tshirley 28 Jan, 2011
Hi,

I have a file upload form which is working well. However I want to rename the uploaded file once it is in the upload folder, with the new name based on some of the other data entered in the form.

Is it possible to use the "extra" code areas to achieve this?

I can write the necessary php code, however I think that I need to have this code run after all other submit processing is complete.

Thanks

Tim
GreyHead 30 Jan, 2011
Hi Tim,

If it is data from the Form inputs then you can add the necessary PHP in the "FileName format box". You are limited to 150 chars in here I think. If you need more than 150 chars then you can include a file there as long as the end result is to declare a value for $filename.

Bob
tshirley 02 Feb, 2011
Thanks,

That should do it but I need to manipulate data a bit more than 150 char will allow. Can you give an example of how I can "include a file in there"?

Sorry, this is probably something very basic in php.

Thanks again,

Tim
GreyHead 02 Feb, 2011
Hi Tim,

Something like this should do it
include(JPATH_SITE.DS.'components'.DS.'com_chronocontact'.DS.'includes'.DS.'file_name.php');

Bob
tshirley 02 Feb, 2011
OK🙂 I understand now, no need to reply.
tshirley 04 Feb, 2011
Sorry trying again...

Ah well, I was pretty confident, but...

The original entry in Filename Format was

$filename = date('YmdHis').'_'.$chronofile['name'];


I created filerename.php as follows:
$months = '123456789ABC';
$days = '123456789ABCDEFGHIJKLMNOPQRSTUV';
$ext = substr($chronofile['name'],-4,4) ;
$datearray = explode("/",$chronofile['date']);
$dayval = substr($days,$datearray[0]-1,1) ;
$monthval = substr($months,$datearray[1]-1,1) ;
$yearval=substr($datearray[2],-1,1) ;
$IGCdate = $dayval.$monthval.$yearval ;
$filename = $IGCdate.'_'.$chronofile['reg'].$ext ;

and changed Filename format to
include 'C:\xampp\htdocs\Benalla2011\components\com_chronocontact\includes\filerename.php' ;

and then got error messages:

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\xampp\htdocs\Benalla2011\libraries\joomla\filesystem\file.php on line 345

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php1DC.tmp' to 'C:\xampp\htdocs\Benalla2011\components\com_chronocontact\uploads\IGC_Upload\' in C:\xampp\htdocs\Benalla2011\libraries\joomla\filesystem\file.php on line 345


I'm puzzled, not sure what the error message means or how I have managed to change the location of the file I am moving.

Cheers

Tim
GreyHead 04 Feb, 2011
Hi Tim,

First silly question: do you have <?php & ?> tags in the filerename.php file? They are needed.

Then please add
gloal $mainframe;
$mainframe->enqueuemessage('$filename: '.print_r($filename, true));
after
$filename = $IGCdate.'_'.$chronofile['reg'].$ext ;
That should show if the file is being included and what value of $filename is being built.

Bob
tshirley 04 Feb, 2011
OK, Thanks,

It is working now. Well, not quite, but it executes my code🙂

Thanks

Tim
tshirley 27 Dec, 2013
This has been working well but now I have upgraded to V4 and there seems to be no longer a Filename Format box that I can use to include the php code I need to rename my files.

Is there some other way that I can make a filename which is not a simple construction from the input fields? In effect I want torun the same code from the earlier posts in this thread.

Cheers

Tim
GreyHead 27 Dec, 2013
Hi Tim,

I suggest that you try my Upload Files [GH] action which will let you set a filename using data from the form (or any other value you set in the $form->data array before the action runs).

Bob
tshirley 28 Dec, 2013
Thanks,

I was able to achieve most of what I need using the File Upload [GH] action.

My remaining issue is that I would like to add a date-time stamp to the filename so that duplicate updates don't overwrite previous files, and so that I have a record of the upload time.

Is there a way to get the current date and time as a default value in a hidden field? That would allow me to add that field in to the filename. Or maybe something else I have not seen yet...

Cheers

Tim
GreyHead 28 Dec, 2013
Hi Tim,

If you scroll down to the bottom of the Help tab in the Upload Files [GH] action you'll see there is a {datetime} value you can use to add a date-time string in 'YmdHis' format e.g. 20110510154324

Alternatively add a custom-code action before the Upload Files [GH] action and add something like this:
<?php
$form->data['time_now'] = date('Y-m-d_His');
?>
and use {time_now} in the filename.

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger