PHP Uploader - Problems with UploadDir option

1

Hello and thanks in advance for the help;)

I'm using the link plugin, for PHP Uploader, and in /php/class.uploader.php I'm trying to change the path where I upload the files:

    private function initialize($field, $options){
		if(is_array($field) && in_array($field, $_FILES)){
			$this->field = $field;
            $this->field['Field_Name'] = array_search($field, $_FILES);
            $this->field['Field_Type'] = 'input';

            if(!is_array($this->field['name'])){
           	$this->field = array_merge($this->field, array("name" => array($this->field['name']), "tmp_name"=>array($this->field['tmp_name']), "type"=>array($this->field['type']), "error"=>array($this->field['error']), "size"=>array($this->field['size'])));
            }

            foreach($this->field['name'] as $key=>$value){ if(empty($value)){ unset($this->field['name'][$key]); unset($this->field['type'][$key]); unset($this->field['tmp_name'][$key]); unset($this->field['error'][$key]); unset($this->field['size'][$key]); } }

            $this->field['length'] = count($this->field['name']);
		}elseif(is_string($field) && $this->isURL($field)){
            $this->field = array("name" => array($field), "size"=>array(), "type"=>array(), "error"=>array());
            $this->field['Field_Type'] = 'link';
            $this->field['length'] = 1;
        }else{
            return false;
        }

        if($options != null){
            $this->setOptions($options);
        }
		$varuser='test';
		//$varuser=$_SESSION['username'];
		$this->options['uploadDir']="../../../uploads/".$varuser."/";
		
		return $this->prepareFiles();
    }

As you can see, at the end of the function I am declaring the "option" UploadDir as the concatenation of "../../../uploads/" + $ varuser + "/", using the code as it is (ie with $ varuser = 'test') everything works correctly.

But, my problem starts when I try to assign the session variable in which I save the connected user, if I use the debugger mode in Chrome I relate the error to the following reference: custom.js: 91 (/ examples / dragdrop / js)

Please, does anyone know what's going on? Why can not I assign the session variable but a variable defined "by hand"?

Thank you very much! Greetings.

    
asked by David Depedro 28.12.2016 в 11:15
source

2 answers

0

We are going to focus on the following line:

$varuser='test';
//$varuser=$_SESSION['username'];

and mainly in $ _SESSION ( documentation 1 , documentation 2 ), in which it indicates that there must be a login in advance.

<?php
     session_start();     
     $_SESSION['username'] = $user_name;
    ?>

Now, or log in, send another parameter in the post or send it as part of the file name

Greetings

    
answered by 28.12.2016 в 15:27
0

In the definition of the initialization of the class is part of the answer:

private function initialize($field, $options){

that is completed near where you are modifying the options by hand

    if($options != null){
        $this->setOptions($options);
    }

So to avoid strange contexts where $_SESSION is not defined, pass the options when creating the instance:

<?php
 session_start();   
 $varuser=$_SESSION['username'];
 $rutaUploads = "../../../uploads/".$varuser."/"; // <- ajusta el path 
 include('class.fileuploader.php');
 // initialize the FileUploader
 $FileUploader = new FileUploader('files', array(
    // Options will go here
    'uploadDir'=>$rutaUploads,
));
  

When using a third-party class always try to use the methods provided by that class to define options, otherwise if you must update the class for a bug or improvement do not lose the changes, and avoid situations like this where by the variable context as $_SESSION may not be available

    
answered by 02.04.2018 в 21:23