Search code examples
phpjquerysessionfile-uploadblueimp

blueimp jQuery-File-Upload. Custom upload folder: update variable before fetching data


I have a problem with the file-upload widget. I'm trying to create a custom folder for each presentation and a sub-folder inside each presentation for each commercial. It goes somehow like this. User chooses/creates presentation -> User chooses/creates commercial inside chosen presentation -> User uploads files -> Folder is create eg. Presentation_1/Commercial_2/uploadedimg.jpg. Next time the user navigates to same presentation and same commercial the uploaded files are shown. Currently I'm using PHP $_Session to select the right data to show depending of the chosen presentation->commercial. The problem is that when page is loaded, the blueimp index.php is also loaded and it sets the variables before you can choose the right presentation->commercial. Therefore it shows the selection you made last time and displays wrong items. How could i force the variables to update after certain button click or delay the loading of UploadHandler.php to happen after button click? There might be some typo's, but the code is working, only one refresh too late! Any help or clues will be appreciated!

App.js

// after button click
    var myData4={"selectedPres":selectedPres,"selectedComm":selectedComm};
    $.ajax({
        url : "./server/php/passSession.php",
        type: "POST",
        data : myData4,
        success: function(data)
        {

        }
    });

passSession.php

<?php
    session_start();

    require_once('connections.php');

    if ($con) {

        mysqli_set_charset($con,"utf8");

        if (isset($_POST["selectedComm"]) && !empty($_POST["selectedComm"])) {
        $selectedPres= $_POST['selectedPres'];
        $selectedComm= $_POST['selectedComm'];

        $_SESSION['selectedPres'] = $selectedPres;
        $_SESSION['selectedComm'] = $selectedComm;
        }

        else {
            echo "Error";
        }

    } else {
        echo "No connection to database";
    }
?>

Index.php

<?php
session_start();
error_reporting(E_ALL | E_STRICT);

if(isset($_SESSION["selectedComm"]) && (!empty($_SESSION["selectedComm"]))) {

    $selectedPres= $_SESSION['selectedPres'];
    $selectedComm= $_SESSION['selectedComm'];

    require('UploadHandler.php');
    $upload_handler = new UploadHandler($selectedPres, $selectedComm);
}
else
    echo "Error in php session.";
?>

UploadHandler.php

function __construct($selectedPres, $selectedComm, $options = null, $initialize = true, $error_messages = null) {
    $this->response = array();
    $this->options = array(
        'script_url' => $this->get_full_url().'/'.basename($this->get_server_var('SCRIPT_NAME')),
        'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/'.$selectedPres.'/'.$selectedComm.'/',
        'upload_url' => $this->get_full_url().'/files/'.$selectedPres.'/'.$selectedComm.'/', ...

Solution

  • Problem solved..just had to update the file list before displaying it.. :D $(".table tr").remove(); $('#fileupload').fileupload({ url: "server/php/" }); $('#fileupload').addClass('fileupload-processing'); $.ajax({ url: $('#fileupload').fileupload('option', 'url'), dataType: 'json', context: $('#fileupload')[0] }).always(function () { $(this).removeClass('fileupload-processing'); }).done(function (result) { $(this).fileupload('option', 'done') .call(this, $.Event('done'), {result: result}); });