• User Newbie

    Upload File - cambiare cartella di destinazione tramite select

    Ciao a tutti.
    Sto implementando una pagina php con upload di file multipli in cui la destinazione dei file viene impostata selezionando un elemento di una dropdown list.
    L' upload viene fatto trascinando l' immagine in un'area apposita e non necessita di bottone submit.

    Il risultato è che non si tiene contro dell' elemento selezionato nella dropdown list e i file vengono uploadati nella folder di default $upload_dir = '../images/', ignorando lo switch case.
    Dove sbaglio?

    Es.
    directory ../images/one/ se viene selezionato l' elemento one della select
    ../images/two/ se viene selezionato l' elemento two della select

    Implementazione:

    <form method="post">
    <div class="styled-select">
    <select id="page" name="select-page[]" required>
    <option value="">Select Page</option>
    <option value="intro">Intro Page</option>
    <option value="story">Story Page</option>
    <option value="sbout">About Page</option>
    <option value="wedding">Wedding Page</option>
    <option value="project">OtherProj Page</option>
    <option value="video">Video Page</option>
    </select>
    </div>

    <div id="dropbox">
    <span class="message">Trascina qui le immagini per l' upload</span>
    </div>
    </form>

    <?php
    $upload_dir = '../images/';

    if (!empty($_POST['select-page'])) {
      $page = $_POST['select-page']; 
      switch ($page) {
    
       case "intro": 
          $upload_dir .= "intro/";
        break;
      
       case "story": 
          $upload_dir .= "story/";
        break;
    
    }
    }
    

    $allowed_ext = array('jpg','jpeg','png','gif');

    if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
    exit_status('Error! Wrong HTTP method!');
    }

    if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){

    $pic = $_FILES['pic'];
    
    if(!in_array(get_extension($pic['name']),$allowed_ext)){
        exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
    }    
    
    
    if(move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])){
        exit_status('File was uploaded successfuly!');
    }
    

    }

    exit_status('Something went wrong with your upload!');

    }
    ?>

    Script Jquery:
    $(function(){

    var dropbox = $('#dropbox'),
        message = $('.message', dropbox);
    
    dropbox.filedrop({
        // The name of the $_FILES entry:
        paramname:'pic',
        
        maxfiles: 5,
        maxfilesize: 2,
        url: '../functions/post_file.php',
    
        
        uploadFinished:function(i,file,response){
            $.data(file).addClass('done');
            // response is the JSON object that post_file.php returns
        },
        
        error: function(err, file) {
            switch(err) {
                case 'BrowserNotSupported':
                    showMessage('Your browser does not support HTML5 file uploads!');
                    break;
                case 'TooManyFiles':
                    alert('Too many files! Please select 5 at most! (configurable)');
                    break;
                case 'FileTooLarge':
                    alert(file.name+' is too large! Please upload files up to 2mb (configurable).');
                    break;
                default:
                    break;
            }
        },
        
        // Called before each upload is started
        beforeEach: function(file){
            if(!file.type.match(/^image\//)){
                alert('Only images are allowed!');
                
                // Returning false will cause the
                // file to be rejected
                return false;
            }
        },
        
        uploadStarted:function(i, file, len){
            createImage(file);
        },
        
        progressUpdated: function(i, file, progress) {
            $.data(file).find('.progress').width(progress);
        }
         
    });
    
    var template = '<div class="preview">'+
                        '<span class="imageHolder">'+
                            '<img />'+
                            '<span class="uploaded"></span>'+
                        '</span>'+
                        '<div class="progressHolder">'+
                            '<div class="progress"></div>'+
                        '</div>'+
                    '</div>'; 
    
    
    function createImage(file){
    
        var preview = $(template), 
            image = $('img', preview);
            
        var reader = new FileReader();
        
        image.width = 100;
        image.height = 100;
        
        reader.onload = function(e){
            
            // e.target.result holds the DataURL which
            // can be used as a source of the image:
            
            image.attr('src',e.target.result);
        };
        
        // Reading the file as a DataURL. When finished,
        // this will trigger the onload function above:
        reader.readAsDataURL(file);
        
        message.hide();
        preview.appendTo(dropbox);
        
        // Associating a preview container
        // with the file, using jQuery's $.data():
        
        $.data(file,preview);
    }
    
    function showMessage(msg){
        message.html(msg);
    }
    

    });

    Grazie a tutti 🙂


  • User Attivo

    Credo che il tuo problema sia il select stesso, invece di:

    
    <select id="page" name="select-page[]" required>
    
    

    prova con:

    
    <select id="page" name="selected_page" required>
    
    

    Di conseguenza modifica questo:

    
    if (!empty($_POST['select-page'])) {
          $page = $_POST['select-page'];
    
    

    con:

    
    if (!empty($_POST['selected_page'])) {
          $page = $_POST['selected_page'];