Skip Navigation

[Resolved] Custom Upload Dir plugin doen't work with Toolset form

This support ticket is created 3 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.10 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by Luo Yang 3 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1808977

Hi, I've installed Custom Upload Dir. I set it to upload files to /uploads/%post_type%/. I've a CPT "candidato" with an upload "CV" custom field. I've creating a new post on the backend and I've uploaded in the custom field the file CV.pdf. The file has been correctly stored in /uploads/candidato/CV.pdf.

Now I've built a Toolset form to do the same on the frontend, but when I upload the file, it is stored in /uploads/CV.pdf.

The form is public and the user is not logged in when fills the form.

Can you help me?

thanks

#1811975

Hello,

There isn't such kind of built-in feature within Toolset Forms plugin, you might consider custom codes, for example:
Use action hook cred_save_data to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

In above PHP function do these:
Get the uploaded file URL:
https://developer.wordpress.org/reference/functions/get_post_meta/
Convert URL value to path value:
https://developer.wordpress.org/reference/functions/wp_make_link_relative/
Move the file to the folder you want:
hidden link
And update the custom field value with new URL value:
https://developer.wordpress.org/reference/functions/update_post_meta/

#1813509

Hi Luo, thanks for the suggestion. This is the code I've set up:

    
    $cv = get_post_meta( $post_id, 'wpcf-cv', true );

    $cv2 = str_replace("/uploads/", "/uploads/candidato/", $cv);
    
    $cv = wp_make_link_relative($cv);    

    $cv_home = str_replace("/wp-content/uploads/", "/home/rubikcom/public_html/wp-content/uploads/", $cv);

    $cv2_home = str_replace("/wp-content/uploads/", "/home/rubikcom/public_html/wp-content/uploads/candidato/", $cv);
    
    rename($cv_home, $cv2_home);

    update_post_meta( $post_id, 'wpcf-cv', $cv2 );

It makes the job but there are some issues:

- The user will upload PDF or DOCX files, I've tested with some PDF and when I download the uploaded file from the frontend, it is a corrupted file that PDF viewer cannot open.

- WordPress is still creating the post on Media with the wrong file URL and it is also creating thumbnails in /uploads/ directory.

I've tried with this code:

   $files = get_attached_media( '', $post_id );

    foreach($files as $file) { 
      $file_id = $file->ID;
    }   
    
    $cv = wp_get_attachment_url( $file_id );

    $cv2 = str_replace("/uploads/", "/uploads/candidato/", $cv);

    $cv = wp_make_link_relative($cv);    

    $cv_home = str_replace("/wp-content/uploads/", "/home/rubikcom/public_html/wp-content/uploads/", $cv);

    $cv2_home = str_replace("/wp-content/uploads/", "/home/rubikcom/public_html/wp-content/uploads/candidato/", $cv);

    rename($cv_home, $cv2_home);

    update_post_meta( $post_id, 'wpcf-cv', $cv2 );
    update_attached_file( $file_id, $cv2 );

It works and it also set the correct URL in the Media post, but it still corrupts the uploaded files and it still creates thumbnails in /uploads/ directory.

Any idea how to prevent file corruption? Is there a way to prevent thumbnail creation or move them to /uploads/candidato/?

Thnaks

#1813549

I suggest you try to get the PHP debug logs, check if there is any PHP errors in your website, and according to our support policy, we don't provide custom support:
https://toolset.com/toolset-support-policy/
When you need custom coding which extends Toolset functionality, we recommend contacting one of Toolset certified consultants.
https://toolset.com/contractors/

#1815781

Hi Luo, I'm almost there. I fixd the both issues I had, this is my code for create form:

    $files = get_attached_media( '', $post_id );

    foreach($files as $file) { 
      $file_id = $file->ID;
    }

    $cv = wp_get_attachment_url( $file_id );

    $cv2 = str_replace("/uploads/", "/uploads/candidato/", $cv);

    $cv = wp_make_link_relative($cv);    

    $cv_home = str_replace("/wp-content/uploads/", "/home/cerocom/public_html/wp-content/uploads/", $cv);

    $cv2_home = str_replace("/wp-content/uploads/", "/home/cerocom/public_html/wp-content/uploads/candidato/", $cv);

    rename($cv_home, $cv2_home);

    update_post_meta( $post_id, 'wpcf-cv', $cv2 );
    update_attached_file( $file_id, $cv2 );

But now I've a problem with the edit form. I set up a code that works fine if I don't change the uploaded file or if I upload a new file by the edit form. But if the new file I update has the same name as the previously uploaded file, the form still uploads the new file to /uploads/.

This is the code for the Edit form (I'm passing 'cv-current' by a hidden field):

    $cv = get_post_meta( $post_id, 'wpcf-cv', true );
    $cv2 = str_replace("/uploads/", "/uploads/candidato/", $cv);
    $cv_current = $_POST['cv-current'];

    $files = get_attached_media( '', $post_id );

    foreach($files as $file) { 
      $file_id = $file->ID;
    }

    $attached = wp_get_attachment_url( $file_id );

    if ($cv2 != $cv_current) {

      $files = get_attached_media( '', $post_id );

      foreach($files as $file) { 
        $file_id = $file->ID;
      }

      $cv = wp_get_attachment_url( $file_id );

      $cv2 = str_replace("/uploads/", "/uploads/candidato/", $cv);

      $cv = wp_make_link_relative($cv);    

      $cv_home = str_replace("/wp-content/uploads/", "/home/cerocom/public_html/wp-content/uploads/", $cv);

      $cv2_home = str_replace("/wp-content/uploads/", "/home/cerocom/public_html/wp-content/uploads/candidato/", $cv);

      rename($cv_home, $cv2_home);

      update_post_meta( $post_id, 'wpcf-cv', $cv2 );
      update_attached_file( $file_id, $cv2 );

    } else {
      update_post_meta( $post_id, 'wpcf-cv', $cv2 );
    }

Is there a way to compare the old stored wpcf-cv with the new one? I haven't find anything on the form that I can use to check it.

cheers

#1815859

There isn't such kind of built-in feature, but there is a workaround, for example, you can setup a hidden generic field "old-wpcf-cv" in your post form and use the wpcf-cv field value as default value, so you will be able to get the old value from PHP POST variable, more help:
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_generic_field
hidden link

#1816139

Hi Luo, yes, this is what I've done, but, if the current uploaded file wpcf-cv value is hidden link (I store this in my generic hidden field 'cv-current') and I upload a new file with the same name 'file1.pdf', when I get it by this code:

$cv = get_post_meta( $post_id, 'wpcf-cv', true );
$cv2 = str_replace("/uploads/", "/uploads/candidato/", $cv);
$cv_current = $_POST['cv-current'];

$cv2 and $cv_current have the same value 'hidden link', but I've uploaded a different file.

How can I check that I'm uploading a new file? I'm sure you use something to check if the form is uploading or not a new file, because when I submit the form without any change on the upload field, you are not performing any action on the uploaded file.

cheers

#1816919

You might consider to compare the file information and modification time, for example:
hidden link
hidden link

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.