简体   繁体   中英

PHP use session vars in class functions

G'day All,

So, I am using jQuery File Upload, and I have it working fine, except for the upload destination, I can set a static path no worres and it works fine, however, I am trying to set the folder path dynamically using a session variable, and I can not seem to get it working, I have been trying for 2 days and can't figure it out...

I found this code for returning the session var if found (note I have changed the false return to '32/Docs/' to give me a false positive check to play with...)

public function getDocsULDIR($DocsULDIRkey)
{
    return (isset($_SESSION[$DocsULDIRkey])) ? $_SESSION[$DocsULDIRkey] : '32/Docs/';
}

The function seems to work in the UploadHandler.php file, as it uploads files to the false positive no worries:

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$this->getDocsULDIR('DBPrimaryKeyRef').'',
'upload_url' => $this->get_full_url().'/'.$this->getDocsULDIR('DBPrimaryKeyRef').'',

So it appears the isset($_SESSION[$DocsULDIRkey]) statement is faulty...

Now, this has me stumped, as I call it via an echo in the code prior to calling the jQuery file upload script and it displays as expected, showing the session variable to be set...

Can anyone please tell me why the public function getDocsULDIR($DocsULDIRkey) does not detect the session variable $_SESSION['DBPrimaryKeyRef'] ?

Thanks in advance.

First, break your problem apart:

1 - try uploading it to a static directory, like /tmp which has write permissions for everyone so you won't get any problems there

2 - tackle your database and session issues, make sure on every upload that your session data is properly set by returning a json to your ajax upload (json_encode($_SESSION)) and make sure all your required variables are there.

3 - make the glue by using your session variables the upload path

just to be sure, I'm seeing you use $_SERVER and $_SESSION vars there but uploads go through $_FILES, are you using it?

Thanks for the input people, turns out I had to call the session insode a protected function referenced within the uploader :)

protected function get_DIR_id() {
    @session_start();
    return $_SESSION["DocsULPrimKey"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM