Helmut.G Helmut.G - 1 month ago 5
PHP Question

PHP - blueimp custom upload directory - empty list on reload

i use blueimp on php. In HTML i've put a hidden input field to submit the User_ID (after login, ID of the user from DB) to the Upload Handler as $_POST value. I use the User_ID value as upload directory name to separate each users files from each other.

HTML:

<form id="fileupload" action="server/php/index.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="user_file_directory" id="user_file_directory" value="<?php echo $_SESSION['user_ID']; ?>">


index.php :

<?php
error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$user_filedirectory = $_POST['user_file_directory'];
$urlHolder = 'user_'.$user_filedirectory.'/';
$options = array(
'upload_dir' => $_SERVER['DOCUMENT_ROOT'].'/ds-admin/server/php/'.$urlHolder,
'upload_url' => './server/php/'.$urlHolder.'/',
);
if($urlHolder){
$upload_handler = new UploadHandler($options , true , null);
}
?>


This works fine to upload something and after upload i can see the thumbnails and even enlarge them. But on a reload of the HTML the list of existing Files is empty, thou the files do still exist on server inside the right folder. On re upload of a file i will see only the new files.

How can i retrieve the content of the custom upload folder on HTML Page load? do i have to set the download-file-directory within the script part of the HTML?

Answer

Solution found!

i only made the mistake not to start the session within the index.php of blueimp. after that everything woks like a flaw! Every user gets his own up/Download directory based on his User-ID from DataBase (saved in Session).

my working index.php looks now like this:

<?php
       /* SESSION START IS IMPORTANT   ;) */
session_start();

/*
 * jQuery File Upload Plugin PHP Example 5.14
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$user_filedirectory = $_SESSION['user_ID'];
$urlHolder = 'user_'.$user_filedirectory.'/';
$options = array(
                'upload_dir' => $_SERVER['DOCUMENT_ROOT'].'/ds-admin/server/php/'.$urlHolder,
                'upload_url' => './server/php/'.$urlHolder.'',
);
if($urlHolder){
    $upload_handler = new UploadHandler($options , true , null);
} 
?>

Lajos Arpad gave me the right hint: $_POST can't work for listing the directory content, for its value will be passed only after a Form-Action... and that is too late.

Comments