solved How to delete one uploaded temp file but keep others from the same form?

SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
My custom module has a form_validate_exit_listener which checks an uploaded image (it must be equirectangular).

If it fails the check, I can do this and everything works as it should :)
global $_post;
jrCore_delete_upload_temp_directory($_post['upload_token']);

But then I've lost other media items which may have been uploaded to other fields.

If I unlink just the failed file the form won't save even with all good files.

Is there any way to get rid of the failed file and notify the user but not insist they upload all the files again, the files can often be quite large.

Any advice or ideas appreciated :) Thanks!


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)

updated by @ultrajam: 08/03/17 01:33:28AM
michael
@michael
7 years ago
7,715 posts
maybe take a look at: jrCore_get_uploaded_media_files()

It uses jrCore_get_upload_temp_directory($_post['upload_token']);

then loops over them and finds the .tmp file in that directory and returns them. If you deleted the unwanted file from the directory that might work.

--- edit --
media.php
/**
 * Get media files that have been uploaded from a form
 * NOTE: $module is no longer used in this function, but was
 * at one time so is left there for backwards compatibility
 * @param string $module Module Name to check file for
 * @param string $file_name Name of file field in form
 * @return mixed
 */
function jrCore_get_uploaded_media_files($module = null, $file_name = null){
    global $_post;
    if (isset($_post['upload_token']{0})) {
        $dir = jrCore_get_upload_temp_directory($_post['upload_token']);
        if (is_dir($dir)) {
            if (is_null($file_name)) {
                $_tmp = glob("{$dir}/*.tmp", GLOB_NOSORT);
            }
            else {
                $_tmp = glob("{$dir}/*_{$file_name}.tmp", GLOB_NOSORT);
            }
            if ($_tmp && is_array($_tmp) && count($_tmp) > 0) {
                foreach ($_tmp as $k => $v) {
                    $_tmp[$k] = substr($v, 0, strlen($v) - 4);
                }
                sort($_tmp, SORT_NATURAL);
                return $_tmp;
            }
        }
    }
    return false;
}

updated by @michael: 04/25/17 02:39:19PM
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Hi Michael

I use that in the function, I probably should have posted the whole thing to start with:
function ujPano_form_validate_exit_listener($_data, $_user, $_conf, $_args, $event)
{ if ($_data['module'] == 'ujPano') {// && isset($_data['pano_equirect_image']) $_fl = jrCore_get_uploaded_media_files('ujPano','pano_equirect_image'); if ($_fl && is_array($_fl) && isset($_fl[0])) { foreach($_fl as $_img) { // check it is 2:1 $_tmp = getimagesize($_img); $w = (int) $_tmp[0]; $h = (int) $_tmp[1]; if ($w / $h !== 2) { // it isn't an equirectangular image global $_post; jrCore_delete_upload_temp_directory($_post['upload_token']); //unlink($_img); jrCore_set_form_notice('notice', "Invalid Equirectangular Image"); jrCore_form_field_hilight('pano_equirect_image'); jrCore_form_result(); } else { } } } } return $_data; }

I've tried deleting the file using unlink($_img) (commented out above) but that results in a continuing failed form validation even when the replacement image passes the equirectangular test. I can't see a way around it.


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
michael
@michael
7 years ago
7,715 posts
a form, with a multi-upload form field.
submit 10 images, but delete #8 for whatever reason and save the other 9.

I'll setup and see if I can see a way.
michael
@michael
7 years ago
7,715 posts
First observation, your using $_post, but its not there without global $_post; which you dont have.

--edit--
yes you are, you're getting it on every itteration of the foreach loop. sorry, see it now. Outside the loop's probably better.
updated by @michael: 04/25/17 06:45:10PM
michael
@michael
7 years ago
7,715 posts
Along with unset($img) also try unsetting the one with the .tmp extension.

                    unlink($_img);
                    unlink($_img .'.tmp');
Got any images that are 2:1 that will pass. need some of those to test further. looking for myself now.
michael
@michael
7 years ago
7,715 posts
This works:

function jrUjPano_form_validate_exit_listener($_data, $_user, $_conf, $_args, $event)
{ global $_post; if (isset($_post['upload_token'])) { $_fl = jrCore_get_uploaded_media_files('jrGallery', 'gallery_image'); if ($_fl && is_array($_fl) && isset($_fl[0])) { foreach ($_fl as $_img) { // check it is 2:1 $_tmp = getimagesize($_img); $w = (int) $_tmp[0]; $h = (int) $_tmp[1]; if ($w / $h !== 2) { // it isn't an equirectangular image //jrCore_delete_upload_temp_directory($_post['upload_token']); unlink($_img); unlink($_img .'.tmp'); jrCore_set_form_notice('notice', "Invalid Equirectangular Image"); jrCore_form_field_hilight('gallery_image'); jrCore_form_result(); } else { } } } } return $_data; }
if you upload 2 images and one of them is 2:1 and the other not, you will be returned to the form and the message shown. If you immediately submit again the 2:1 image will still be attached and will be saved.

Suggest the message read something like "picture_of_a_cat.png was not 2:1 so has been removed from your list. Submit again to save remaining files."

OR just remove it and continue the submit saving all the 2:1 images and if there is a non-2:1 image save a temp value with jrCore_set_temp_value() and make a smarty function to check that set temp value and show a message on the landing page "all images that were not 2:1 have been removed"
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Ah, the .tmp image!!!!!!

Thanks Michael, that works perfectly :)


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
michael
@michael
7 years ago
7,715 posts
:)

Tags