SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing Session Variables within a class

    Hi

    I am trying to utilise a jQuery file upload script( https://github.com/blueimp/jQuery-File-Upload ) to upload multiple images to the server. The php upload script needs to put the images into different folders based on user id and building id. The upload script is integrated into a larger form that records various details about the images and ultimately stores this information with the db. Standalone the script works fine but when integrated within the larger form the I am unable to access the session variable from within the upload class. The following is the offending code. I can confirm that the session variables selected_building and user_id are set but are null within the class. Any help would be greatly appreciated as I have spent about four days trying to resolve this issue.

    Regards


    Jen

    include("include/session.php");
    class UploadHandler
    {
    private $options;
    function __construct($options=null) {
    $selected_building = ($_SESSION['selected_building']);
    $user_id = ($_SESSION['user_id']);
    $prop_id = explode('_', $selected_building );
    $sel_prop = $prop_id[0];

    $this->options = array(
    'script_url' => $_SERVER['PHP_SELF'],
    'upload_dir' => dirname(__FILE__).'/client_files/'. $user_id . '_' . $sel_prop .'/images./',
    'upload_url' => dirname($_SERVER['PHP_SELF']).'/client_files/'. $user_id . '_' . $sel_prop.'/images/' ,
    'param_name' => 'files',
    // The php.ini settings upload_max_filesize and post_max_size
    // take precedence over the following max_file_size setting:
    'max_file_size' => null,
    'min_file_size' => 1,
    'accept_file_types' => '/.+$/i',
    'max_number_of_files' => null,
    'discard_aborted_uploads' => true,
    'image_versions' => array(
    // Uncomment the following version to restrict the size of
    // uploaded images. You can also add additional versions with
    // their own upload directories:
    /*
    'large' => array(
    'upload_dir' => dirname(__FILE__).'/files/',
    'upload_url' => dirname($_SERVER['PHP_SELF']).'/files/',
    'max_width' => 1920,
    'max_height' => 1200
    ),
    */
    'thumbnail' => array(
    'upload_dir' => dirname(__FILE__).'/client_files/'. $user_id . '_' . $sel_prop .'/images/thumbnails/',
    'upload_url' => dirname($_SERVER['PHP_SELF']).'/client_files/'. $user_id . '_' . $sel_prop .'/images/thumbnails/',
    'max_width' => 80,
    'max_height' => 80
    )
    )
    );
    if ($options) {
    $this->options = array_replace_recursive($this->options, $options);
    }
    }

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hy
    Normally, Sessions can be read inside classes, if you add var_dump($_SESSION['user_id']); in the class and returns Null, it is posible to have some code /other script that destroys the session before using that class.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi MarPlo

    Thanks for your reply. Your were correct, I had to go back to basics and strip out the master page and specifically set the session variables and hey presto the script finally works as intended.

    Thanks for your help

    Jen


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •