cformsII Support Forum
Current User: Guest *Frequently Asked Questions*
Search 
Search Forums:


 




Rename file attachment based on variables!

Add a New Topic Reply to Post
Post

affa - Guest

11:54 pm - March 14, 2008

Please replace the ??? with your data!

  • Your URL: http://???
  • The browser used: ???
  • cforms version: latest
  • Your Wordpress version: latest

[desc]:

We would LOVE it if we could have the file name submitted via file upload automatically based on the variables the user entered (much like the subject line!).  We use the form to get lots of small files from an assortment of people, and few of them name their files well.  It would be wonderful if we could change the filename submitted based on the variables they entered.

_

Oliver - Admin

8:05 am - March 15, 2008

posts 3553

That is something you can code in 'my_cforms_action'.

This action hook gets triggered in lib_nonajax.php shortly after the upload files have been placed into the corresponding form directory.

Take a look at the code underneath

    //
    // Files uploaded??
    //

and simply copy/paste parts that you need to identify each file and rename it.

Of course you could also hard code it directly there, but it be cleaner if you used the action hook.


affa - Guest

11:25 pm - March 22, 2008

cool - we will look into this.


would this code need to be replaced after every update?


another option we were considering was somehow using the dynamic form option to display the correct file name before the user hits submit, though I'm not sure this is actually possible yet (I just came up with the idea and haven't actually researched dynamic form possibilities).


Oliver - Admin

10:11 am - March 23, 2008

posts 3553

would this code need to be replaced after every update?

You certainly need to keep a backup of this file including your custom code, because if you do update (manually or especially automatically via one-click installer) it would overwrite your file.

Simply *before* upgrading, make a backup and copy it back into place after updating.

dynamic form option to display the correct file name before the user hits submit,

Dynamic forms are created before user input, so it wouldn't know about any filenames at the time of rendering. Plus it's always the user who determins the file name at the time of upload, *after* the upload it is up to you.

Sean - Guest

6:37 pm - November 5, 2008

First, thank you for this excellent plugin!


I have been trying to get this to work (renaming an uploaded file by field input values).


To start with I have been trying to hard code a simple example into lib_noajax.php. Starting with a very simple example of renaming the uploaded file to the submitter's name using the following code:


$form   = $cformsdata['data'];

followed later by:

move_uploaded_file($tmpfile,$fileuploaddir.'/'.$form['Your Name']);


Where 'Your Name' is a field name in the form. Is this the correct syntax to access that data? As far as I can tell, $form['Your Name'] has a blank value. Sorry, if this is completely naive! :)

Oliver - Admin

7:18 pm - November 5, 2008

posts 3553

If you want to rename upload files, I recommend to wait for v9.2 (I can send you a beta copy) it comes with a function in my-functions.php which can be used to do that.

If you just do the above, you may be able to change the filename but risk of not being able to access it later via the admin GUI.

Sean - Guest

9:33 pm - November 5, 2008

Thanks for the quick response!

I would love to try out a 9.2 beta version for the new function you mentioned.

If possible, could you send a link/copy to the e-mail address linked to this guest account?

Cheers,

Sean

Oliver - Admin

7:52 am - November 6, 2008

posts 3553

I'm working on closing one more open item, but will send it your ways once that's done.

Sean - Guest

10:20 pm - November 7, 2008

Thank you for the code. This looks promising (and I see you just released 9.2); however, this still brings me back to one basic question.

How do I correctly refer to a variable from a field at this stage in the submission process?

If I have a field called "Your name", and that is how it appears in the SQL database, can I refer to its value as the following?

$cformsdata['Your name']

It would seem this is incorrect.

Oliver - Admin

10:21 am - November 8, 2008

posts 3553

If in doubt what the variable name is and how to reference it, simply uncomment the @mail() example in the code and have cforms send you an email with the 'exploded' array which will show you exactly how to reference the data.

Sean - Guest

6:47 pm - November 8, 2008

OK, done that.

The result looks like this:


Array
(
   [$$1] => Fieldset1
   [Fieldset1] => Some text here
   [$$2] => Your Name
   [Your Name] => SomeOnesName
   
etc...
   [$$5] => Upload your file [*]

   [Upload your file [*]] => originalfilename.txt

)
So, within my-functions, how do I correctly reference the [Your Name] field?
Something like this?
if ( $setting == "filename" ){
return $cformsdata['data'][$cformsdata['data']['Your Name']] . $oldvalue;
}
or
if ( $setting == "filename" ){
return $cformsdata['data'][$cformsdata['data']['$$2']] . $oldvalue;
}
or
if ( $setting == "filename" ){
return $cformsdata['Your Name'] . $oldvalue;
I've tried every combination I can think of, but 30 tries later, I still can't get it to work.
The function is getting called correctly, because if I just manually enter a value instead of getting it from $cformsdata, it works.
I see the comments just below in my-functions:
// note: '$$mypick' references the ID of the HTML element and has been assigned
// to the drop down field in the form configuration, with [id:mypick] !

Although I can't figure out the syntax to specify the id of a field in the form configuration. I assume because it hasn't been set in the configuration, the id for "Your Name" is 2 here? What syntax would I use? Is this necessary?

Thanks!

Sean

Oliver - Admin

7:09 pm - November 8, 2008

posts 3553

This should have worked:

$cformsdata['data'][$cformsdata['data']['$$2']]

and so should:

$cformsdata['data']['Your Name']

Did you add the @mail() test code to the logic function (by default I think it's only in the filters)?


Sean - Guest

8:41 pm - November 8, 2008

No, I had left it in the my_cforms_action function. Good point.

Actually, if I add it within the my_cforms_logic within the filename "if" statement like this:


if ( $setting == "filename" ){

$form   = $cformsdata['data'];

@mail('myemail@email.com', 'cforms my_action test', print_r($form,1), 'From: anotheremail@email.com');

return $cformsdata['data'][$cformsdata['data']['$$$2']] . $oldvalue;

}

The email that gets sent from @mail is blank. Has $cformsdata['data'] been erased by the time this function is called?

If it's left outside of the filename "if" statement then I do get a value emailed to me.


Oliver - Admin

9:44 am - November 9, 2008

posts 3553

Right, I believe at the time the 'filename' exception gets triggered (and this function called),  $cformsdata is not set, so you'd have to fall back to the $_POST array:

$form   = $_POST;

Sean - Guest

4:42 pm - November 9, 2008

Yes - that's it!

In case it helps someone else in the future, here's a complete example using the data from the 2nd field and 2nd form:

function my_cforms_logic($cformsdata,$oldvalue,$setting) {

  if ( $setting == "filename" ){

    $form   = $_POST;

    // uncomment to get an email and check the field values:

    // @mail('youremail@yourwebsite.com', 'cforms my_action test', print_r($form,1), 'From: admin@yourwebsite.com');

    return $form['cf2_field_2'] . $oldvalue;

  }

}

Thanks for your help!


Add a New Topic Reply to Post


Reply to Topic: Rename file attachment based on variables!
PLEASE READ THE FAQs FIRST! THANK YOU.

NOTE: New Posts are subject to administrator approval before being displayed

Guest Name (Required):

Guest EMail (Required):

Guest URL (required)

Math Required!
What is the sum of: 6 + 2        (Required)

Topic Reply:


 
© Simple:Press Forum - Version 3.1.3 (Build 356)