Skip Navigation

[Gelöst] validate file data structure on upload – cred_form_ajax_upload_validate

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

This support ticket is created vor 3 Jahren, 3 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von davidZ-4 vor 3 Jahren, 3 Monaten.

Assistiert von: Luo Yang.

Author
Artikel
#2119663
file path issue.jpg
file path issue 3 - with var-dump.jpg
file path issue 2.jpg

this is the same issue of https://toolset.com/forums/topic/validate-file-data-structure-on-upload-cred_form_ajax_upload_validate/
I just couldn't add this comment and reopen the topic.

Hi Lou.

I have run this code as you suggested and asked my team to look into this as well. we all get the same issue.
we don't get the full path as you did in your screenshot above.
see attached 'file path issue.jpg' .

The "temp_name" string is not complete and does not include the full URL.
Also, I see that in your screenshot the file is extension is tmp and not the actual file extension and the name is a random name.

Our goal is to validate file extension and structure of excel, csv and txt files. we can do the extension validation just fine.
Once the extension is validated we are trying (in the same function) to read the file and validate its structure. it seems that are having some issue with reading the file as we don't get the actual path and file extension.

I'm trying to understand if the temp file with the temp name and .tmp extension is readable or not. Is the tmp folder in the path is virtual? i.e. is it been created in the virtual memory of the server. I ask this as I couldn't find the tmp folder on the server.

Step 1 - 1b of our process works fine:
1. Validate file extension is correct (using : cred_form_ajax_upload_validate)
1a. if error - deny upload, ask user to upload file with acceptable file extension.
1b. if no error - go to step 2

the next step is not successful (within the same function)
2. get file path and validate file data structure
I believe we cannot get the file path as the

return array( $fields, $errors );

is not returned yet.
the next two sub steps are depended on the previous step
2a. if error - deny upload, ask user to correct the file data structure and try again (go to step 1 on ne upload).
2b. in no error - accept the file and continue to the form (so user can complete the form submitting)

I noticed that if we don't go to step 2 within the same function and the upload is successful we can get the file absolute path. see file 'path issue 2.jpg' image

BTW
when I us the var_dump($fields); in the function I always get an error even if the file extension is correct. see 'file path issue 3 - with var-dump.jpg'.

Any advice on this will be appreciated.
Thanks,

David

#2119727

Hello,

In the screenshots you provided above:
https://toolset.com/wp-content/uploads/2021/07/2119663-file_path_issue_3.jpg
https://toolset.com/wp-content/uploads/2021/07/2119663-file_path_issue_3_with_var_dump_2.jpg
There is one item "tmp_name" in the var_dump result, that is file path, you can get it's value like this:

$filepath = $fields['wpcf-u-sngl-portfolio-file']['field_data']['tmp_name'];

Please replace "u-sngl-portfolio-file" with your custom field slug.

Then you can open it with other PHP function, for example file_get_contents():

$content = file_get_contents($filepath);

versteckter Link

And do the custom validation as what you want: structure of excel, csv and txt files

#2120791

My issue is resolved now. Thank you!