SOLVED: Problem with upload files in Form element

Browser: Edge
OS: Windows

When upload files and add a custom action… The file doesnt upload correctly. I get the URL but the image never uploads:

/**
 * FORM TEST
 */
add_action( 'bricks/form/custom_action', 'url_test', 10, 1 );

function url_test( $form ) {
  $form_fields   = $form->get_fields();
  $settings = $form->get_settings();
  $form_id       = $form_fields['formId'];
  
  $current_user = wp_get_current_user();
  $user_id = $current_user->ID;
  
  // Check if the form id is the one you want
  if( $form_id !== 'gfseuh' ) return;
  
    // Obtener los archivos subidos desde el formulario
    $uploaded_files = $form->get_uploaded_files();
    
    // Verificar si el campo "form-field-sajbjc" existe en los archivos subidos
    if (isset($uploaded_files['form-field-wfkglc'])) {
        // Acceder al primer elemento del campo "form-field-sajbjc" para obtener la URL
        $url = $uploaded_files['form-field-wfkglc'][0]['url'];
        // Actualizar el campo de tipo imagen del usuario actual
        update_field('test_url', $url, 'user_' . $user_id);
    } else {
        $url = 'Nada :(';
        // Actualizar el campo de tipo imagen del usuario actual
        update_field('test_url', $url, 'user_' . $user_id);
    }

}

I created an text field called “test_url” and it receives the url, but the files in this no exists. Any idea?

1 Like

Hey @noobato,

thanks for your report.

I was able to reproduce the issue and added it to our bug tracker.

Best,

André

1 Like

Thank you for this reply! It was driving me crazy…

Anyway, I’m working in this site and need to be finished soon. Is there a workaround that I can use meanwhile? I can’t use any plugin for this.

Thank you, again, in advance :slight_smile:

Hi @aslotta :slight_smile: any update ? thank you in advance

2 Likes

Any update on this issue?
We’re having the same issue. However in our case, the file is uploading but to a different URL.

For instance,
this is the array I get

(
            [0] => Array
                (
                    [file] => /home/eyva6dt/public_html/wp-content/uploads/2023/10/FBL-Redemption-Partner-List-September.pdf
                    [url] => https://hanzo.com/wp-content/uploads/2023/10/FBL-Redemption-Partner-List-September.pdf
                    [type] => application/pdf
                )

        )

from get_uploaded_files, but the file is being uploaded at a different URL:
/wp-content/uploads/2023/10/gszqky651ee1bb970d26.91396348_FBL-Redemption-Partner-List-September.pdf

Hi @noobato @ahsanbod ,

We’ve fixed this issue in Bricks 1.9.2 beta, now available as a manual download in your account (see changelog).

Please let us know if you are still experiencing issues.

As with any beta release, please do not use it on any production/live website. It is only meant for testing in a local/staging environment.

Hi, @timmse ,

After downloading Bricks 1.9.2 beta I’m still having a very similar issue. I’ve created a custom action to send form data to Telegram. When sending and checking the file URL after it was sent, that file doesn’t exist.

Here’s a piece of what I have:

function telegram_send( $form ) {

    // Get information about uploaded files
    $uploaded_files = $form->get_uploaded_files();

    // Check if there are uploaded files
    if (!empty($uploaded_files)) {
        $telegram_message .= "Uploaded files:\n";
        foreach ($uploaded_files as $field_id => $files) {
            foreach ($files as $file) {
                // Add only the file URL to the message
                $telegram_message .= $file['url'] . "\n";
            }
        }
    }
}

Looking forward to your suggestions. Thank you.

Hi @Maxim ,

May I know did you activate the settings here?

Regards,
Jenn

Hi, @itchycode ,

Thank you. I did not, but I have now. And it works when I choose to save the file in media library, but doesn’t work when I save the file in custom directory.

When saving the file in custom directory the sent URL looks like this /wp-content/uploads/uploaded-file.jpg. It’s missing /custom-directory/.

Hi @Maxim ,

As this is your custom code, please print_r or var_dump the $file inside your second foreach loop.
You should be able to find the physical location of the file in $file['file']

The $file['url'] is just a temp location upon upload.

If you still encounter issues, I would appreciate it if you could create a new thread :slight_smile:

Regards,
Jenn

1 Like

Hi, @itchycode ,

I have switched over from [‘url’] to [‘file’], but the outcome is the whole directory where this image lies. (/var/www/vhosts/domain.com/httpdocs/wp-content/uploads/profileimg/Screenshot_119.jpg)

The problem is, that way I can not use this to display dynamic images inside bricks.

What I would need is: domain.com/wp-content/uploads/profileimg/Screenshot_119.jpg

Are there any workarounds for this?
Best
Tobi

Hi @tobi ,

Every website setup has different way to organize their WP site.
The upload folder structure in WordPress may vary from one site to another; some may have custom directories that are not directly accessible. Some may not inside uploads folder as well. Hence, we cannot build exact final accessible URL from the function.

If you are using default structure, this code should be able to build the URL.

$upload_dir = wp_upload_dir();
$file_url   = str_replace( $upload_dir['basedir'], $upload_dir['baseurl'], $file['file'] );

Regards,
Jenn