1

I have this code for download my files :

$id = '251';
$type = 'download';
$post_type = 'news';
$file_name = 'C_3.docx';

$files = _files_list_($id,$type);

print_r($files);

array_walk_recursive($files, function ($value) {

    if (false !== stripos($value, $file_name)) { // LINE 17
        $file = ABSPATH.'/uploads/files/'. _is_file_author($id,$type,$post_type).'/'.$value.'';
        ( new Downloader( $file ) )->download();
        echo $file;
    }
});

print_r($files) :

Array ( [0] => Array ( [0] => docs/manager.zip ) [1] => Array ( [0] => docs/C_3.docx ) )

But in Action i see this error (not detect $id , $type, $file_name, $post_type):

Notice: Undefined variable: file_name in C:\xampp\htdocs\cmd\modules\download.php on line 17

how do fix this error?

7
  • 7
    $file_name is out of scope here. You need to use it. So do something like this ..function($value) use ($file_name). And as @DeDee said, it could very well be undefined. But in the context above, it's just out of scope. Commented Aug 31, 2015 at 10:18
  • andrew, post it as answer please Commented Aug 31, 2015 at 10:19
  • 1
    Eh, I'm good. Imaginary internet points don't really tickle me right anyway. Commented Aug 31, 2015 at 10:20
  • @Andrew: error fix, but not detect $id , $type, $file_name, $post_type in array_walk_recursive Commented Aug 31, 2015 at 10:27
  • Makes sure you use all the variables and also make sure they contain some sort of value. Commented Aug 31, 2015 at 10:28

1 Answer 1

1

@Andrew post good comments and @briosheje send complete comments:

i change

array_walk_recursive($files, function ($value)

to

array_walk_recursive($files, function ($value) use ($file_name, $post_type, $type, $id),

this worked now.

Sign up to request clarification or add additional context in comments.

1 Comment

Also note that, superglobals like $_GET, $_POST, etc... are accessible without passing with use. So, you can directly use them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.