I'm trying to retain uploaded file info while changing other form toggles/parameters and avoid having to re-upload the file each time. Is it possible to persist the file upload data?
HTML for form
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="fileUpload" id="fileUpload">
<input type="submit" value="Upload This" name="upload">
<input type="submit" value="Preview" name="preview">
<div class="col text-center justify-content-center">
<div class="bmd-form-group">
<label class="bmd-label-static" for="txtcolor">Choose the Text Color</label>
<input type="color" id="txtcolor" name="txtcolor" value="#000000">
<label for="body">Body</label>
</div>
</div>
<div class="col text-center justify-content-center">
<div class="form-check">
<label class="form-check-label">
<input id="iversion" class="form-check-input" name="invertBox" type="checkbox" value="">Invert
<span class="form-check-sign">
<span class="check"></span>
</span>
</label>
</div>
</div>
<?php
if($previewOk){
echo "<img class='w-100 h-100' src='data:image/jpeg;base64, $imgData' />"; ?>
<?php } ?>
<a href="<?php $add_to_cart = do_shortcode('[add_to_cart_url id="'.$post->ID.'"]'); echo $add_to_cart;?>"class="more">Buy now</a>
</form>
PHP
$previewOk=0;
if($_FILES["fileUpload"]["tmp_name"]){
$myfile=$_FILES["fileUpload"]["tmp_name"];
}
if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
$file_url = move_file_test(); //moves file to directory
$theid = create_download(); //creates a file link
create_download_version($theid, $file_url); //more file stuff
}
if(isset($_POST['txtcolor'])){
$color = $_POST['txtcolor'];
$myfile = change_color($myfile); //some function that alters the $myfile and regenenerates
}
if(isset($_POST["preview"])) {
$imgData = base64_encode(file_get_contents($myfile));
previewOk = 1;
}
I'd like $myfile
to stick around after the form is submitted and only update when uploading a different file. The intention is to access $myfile
again in some other form based action - for example: changing the background color of an uploaded image file and previewing it without having to launch a file browser and start over each iteration.
UPDATE
Added some additional context to further illustrate. $myfile
persistence would be nice for previewing the file (let's say it is an image file) and also for performing form specified transforms (such as applying updates from the color picker form input)
Integration with woocommerce (in case the image is for purchase) is causing some headaches due to my limited experience with wordpress hooks and actions. I would like to set and then pass some parameters from the form during the add_to_cart
shortcode which will have a hook into the woocommerce_checkout_update_order_meta
method, but the form submission and the shortcode do not share any namespace, and the form resubmission wipes the variables I'd want to pass to the hook (once I figure out closures?)
I have not tried Ajax, but I have a feeling it is probably the best solution.