1

I am using JQuery File Upload plugin to make add the possibility to upload files to my website.

The upload script is in a page like "index.php?cartella_id=x" (x is a number that indicated a album ID).

I would like to store files like this:

server/php/files
- directory 1/
- - Image x
- - Image y
- directory 2/
- - Image z
- - Image z(2)

I basically want to create a different directory for each album.

Storing the images like I want is not hard because I pass $cartella_id by using a hidden input in the form like this

<input type="hidden" name="cartella_id" value="<?php echo $cartella_id; ?>">

In the server/php/index.php file I check if the user is logged in and if the album exists like this

session_start();
if(!isset($_SESSION["username"])) {
    die();
    }


if(isset($_REQUEST["cartella_id"])) {
    $cartella_id = (int) $_REQUEST["cartella_id"];
    $sql = "SELECT * FROM cartelle WHERE cartella_id = $cartella_id";
    $query = mysql_query($sql);
    if($cartella = mysql_fetch_array($query)) {
        $upload_dir = '/files/'.$cartella_id.'/';
        } else {
        die();
        }
} else {
    die();
}

And in the UploadHandler.php page I edit the 'upload_dir' and 'upload_url' options.

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/'.$_REQUEST["cartella_id"].'/',
            'upload_url' => $this->get_full_url().'/files/'.$_REQUEST["cartella_id"].'/',

Upload works fine... the problem is that if I refresh the upload page I can't see already-uploaded files like the script would show me when no custom path is specified. I can use $_SESSION to fix this problem but I don't like this solution and the Delete buttons wouldn't work in any case.

I studied the PHP code a lot and I've also googled a lot but I couldn't find a solution that works for me.

How do I send custom variables when the script is checking for existing files (I couldn't find that piece of code)? How do I make the Delete buttons work?

Thanks in advance

2 Answers 2

1

So I solved the first problem (make files visible even after reloading the page) by editing js/main.js. I edited this:

url: $('#fileupload').fileupload('option', 'url'),

to this

url: ($('#fileupload').fileupload('option', 'url') + "?cartella_id="+ document.getElementById('cartella_id').value),

Still have to make the delete buttons work though.

Sign up to request clarification or add additional context in comments.

Comments

0

I had the same problem and i solved it enabling the user-directories and overriding the methods in the class UploadHandler as suggested here:

jQuery-File-Upload PHP-user-directories

I added an extra parameter to the delete url.

My index.php :

<?php

error_reporting(E_ALL | E_STRICT);

require('UploadHandler.php');

class CustomUploadHandler extends UploadHandler {

    protected function get_user_id() {
        return $_REQUEST['recordId'];
    }

    protected function set_additional_file_properties($file) {
        $file->deleteUrl = $this->options['script_url']
            .$this->get_query_separator($this->options['script_url'])
            .$this->get_singular_param_name()
            .'='.rawurlencode($file->name)
            .'&recordId='.$_REQUEST['recordId']
            ;
        $file->deleteType = $this->options['delete_type'];
        if ($file->deleteType !== 'DELETE') {
            $file->deleteUrl .= '&_method=DELETE';
        }
        if ($this->options['access_control_allow_credentials']) {
            $file->deleteWithCredentials = true;
        }
    }   

}

$upload_handler = new CustomUploadHandler(array(
    'user_dirs' => true,
));

and i changed the url in

my main.js:

$(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        url: 'server/php/index.php?recordId=' + recordId,
    });

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );

        // Load existing files:
        $('#fileupload').addClass('fileupload-processing');
        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            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});
        });

});

}

i have not edited the UploadHandler.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.