Search code examples
phpimage-uploadingjquery-file-upload

JQuery File Upload Custom Path Problems


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


Solution

  • 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.