Rename uploaded file on form

JHSPerc

New Member
So I thought if I did a submission php plug-in like this:

PHP:
$lname = JRequest::getVar('freshsa___lname');
$fname = JRequest::getVar('freshsa___fname');
$minitial = JRequest::getVar('freshsa___minitial');
$essayupload = JRequest::getVar('freshsa___essayupload');

$old = ''.$essayupload.'';
$new = '../public_html/applications/'.$lname.'_'.$fname.'_'.$minitial.'';

rename($old, $new) or die ("Unable to rename $old to $new.");

$old1 = '../public_html/applications/'.$lname.'_'.$fname.'_'.$minitial.'';
$new1 = '../applications/essays/'.$lname.'_'.$fname.'_'.$minitial.'';

copy($old1, $new1) or die ("Unable to copy $old1 to $new1.");

The file would be renamed the way I want it and it would be in the location I wanted. Well, alas it is not. What do I need to do in order to make this work?
 
So I thought if I did a submission php plug-in like this:

PHP:
$lname = JRequest::getVar('freshsa___lname');
$fname = JRequest::getVar('freshsa___fname');
$minitial = JRequest::getVar('freshsa___minitial');
$essayupload = JRequest::getVar('freshsa___essayupload');

$old = ''.$essayupload.'';
$new = '../public_html/applications/'.$lname.'_'.$fname.'_'.$minitial.'';

rename($old, $new) or die ("Unable to rename $old to $new.");

$old1 = '../public_html/applications/'.$lname.'_'.$fname.'_'.$minitial.'';
$new1 = '../applications/essays/'.$lname.'_'.$fname.'_'.$minitial.'';

copy($old1, $new1) or die ("Unable to copy $old1 to $new1.");
The file would be renamed the way I want it and it would be in the location I wanted. Well, alas it is not. What do I need to do in order to make this work?

Hi JHSPerc,

What Fabrik/Joomla version are you using?

Did you do var_dump on all your variables and things showing up correctly?

What exactly is happening? Is anything being saved in the database? Besides the file not appearing in FTP?
 
Use the Joomla and Fabrik API's.

Put

Code:
jimport( 'joomla.filesystem.file' );
at the start of your code and use

Code:
JFile::move( $oldname, $newname );
For the rename.

You can also do

Code:
JFile::move( JPATH_SITE . '/applications/' $dirfiles, JPATH_SITE . '/whatever/' . $newfile );
For the correct filepaths, which is a slightly better way of doing it.

You may also have to split the extension first depending on how you are doing you renaming.

Code:
$oldfilearray  = pathinfo( $oldfile );
$oldfilearray will become an array of your file parts.

$oldfilearray['dirname']
$oldfilearray['basename']
$oldfilearray['extension']
$oldfilearray['filename']

So renaming then becomes....

Code:
JFile::move( JPATH_SITE . '/applications/' . $oldfilearray['filename'] . '.' . $oldfilearray['extension'],  JPATH_SITE . '/applications/essays/' . $newfile . '.' . $oldfilearray['extension']);

$oldfile[ 'extension' ];
or something like that anyway.... :)

I have got a half written article about this as I was doing something simliar for somebody with Docman.... just finding the time to finish it.

As Tessa says though it's helpful to dump your values line by line if something doesn't work.



 
Thank you felixkat - I will try that and see if it works. Question - would using the Joomla and Fabrik API's allow me to move the files outside of their respective working directories?

Tessa - I have the most up-to-date Joomla and Fabrik. When I did my dump everything except essayupload is there. I don't understand why its a blank after the form has been submitted. If I check the table the proper value is listed.

An error is occuring due to no value coming from essayupload. All of the values from my form are being saved in the database and are populating the other fields .
 
No not really, I was just wondering. :) It does make a difference to the way certain things are processed, but it's not important here with the single file upload.

Have a look at the Wiki entry for accessing the formdata.

http://fabrikar.com/wiki/index.php/Form_plugin_php



Although you can use placeholders I tend to put any scripts into a file and access the values with $formModel->_formData

Dumping this value at the beginning will show you all the values

echo '<pre>'; print_r($formModel->_formData); exit;

This would be the quickest way to see if all your values are there.
 

Members online

No members online now.
Back
Top