oldtimer
oldtimer

Reputation: 11

access to array outside function

Have found 2 threads on this here at Stackoverflow, hovever these are about how to return an array. I can generate an array inside the function and it is having the right content, however when doing var_dumping of $target_file outside the function it is NULL. Otherwise the code is doing what it is supposed to do. Is it a scope thing? or...have I done something completely wrong here? Can anyone please help me access the returned array outside the function?

function copy_files3($requested, $src_path, $send_path){
//function copy_files renames and copies pdf-files to a specific folder. 
//ARRAY $requested (keys INT), (names STR) holds the names of the selected files
//STR $src_path is the full path to the requested files
//STR $send_path is the full path to the re-named files
//ARRAY $target_file holds the names of the renamed files
    $i=0;
    $target_file = array();
    $src_filename = array();
    $b=array();
    foreach($requested as $value) {
        //$value holds the names of the selected files. 
        //1 Expand to get the full path to the source file
        //2 Generate a 10 char + .pdf (aka 14 char) long new file name for the file.
        //3 Generate full path to the new file.
        $src_filename[$i] = $src_path.$value;
        $rnam[$i] = randomstring(); //function randomstring returns a 10 char long random string
        $target_file[$i] = $send_path.$rnam[$i].'.pdf';
        echo 'target_file['.$i.'] = '.$target_file[$i].'<br>';
        copy($src_filename[$i],$target_file[$i]);
        $i++;
    }
    return($target_file);
} 

I have files renamed and placed in the correct folders on my server, the only problem is accessing the $target_file array after this function.

Upvotes: 0

Views: 844

Answers (2)

Hardik Kalathiya
Hardik Kalathiya

Reputation: 2253

<?php 

function copy_files3($requested, $src_path, $send_path){
//function copy_files renames and copies pdf-files to a specific folder. 
//ARRAY $requested (keys INT), (names STR) holds the names of the selected files
//STR $src_path is the full path to the requested files
//STR $send_path is the full path to the re-named files
//ARRAY $target_file holds the names of the renamed files
    $i=0;
    $target_file = array();
    $src_filename = array();
    $b=array();
    foreach($requested as $value) {
        //$value holds the names of the selected files. 
        //1 Expand to get the full path to the source file
        //2 Generate a 10 char + .pdf (aka 14 char) long new file name for the file.
        //3 Generate full path to the new file.
        $src_filename[$i] = $src_path.$value;
        $rnam[$i] = randomstring(); //function randomstring returns a 10 char long random string
        $target_file[$i] = $send_path.$rnam[$i].'.pdf';
        echo 'target_file['.$i.'] = '.$target_file[$i].'<br>';
        copy($src_filename[$i],$target_file[$i]);
        $i++;
    }
    return($target_file);
} 

$returnedValue = copy_files3($requested, $src_path, $send_path);
?>

Upvotes: 0

Jakub Matczak
Jakub Matczak

Reputation: 15656

$target_file exists only inside the function. And yes, it's a scope thing.

You are returning this variable's value, but not the variable itself.

All you have to do is to assign returned value to some variable while invoking the function.

$returnedValue = copy_files3($requested, $src_path, $send_path);

Now, $returnedValue has the same value as $target_file had inside the function.

Upvotes: 5

Related Questions