2016-07-20 5 views
0

Обнаружили здесь 2 потока в Stackoverflow, о том, как вернуть массив. Я могу сгенерировать массив внутри функции и иметь правильный контент, однако при выполнении var_dumping $ target_file вне функции он равен NULL. В противном случае код выполняет то, что он должен делать. Это предмет видимости? или ... я сделал что-то совершенно неправильное здесь? Может ли кто-нибудь помочь мне получить доступ к возвращенному массиву вне функции?доступ к массиву вне функции

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); 
} 

У меня есть файлы переименованы и помещены в соответствующие папки на моем сервере, единственная проблема доступа массив $ целевой_файл после этой функции.

+0

Пожалуйста, покажите код, где вы вызываете функцию, и попытаться использовать возвращаемое значение – Steve

ответ

5

$target_file есть только внутри функция. И да, это предмет охвата.

Вы возвращаетесь значение этой переменной, но не сама переменная.

Все, что вам нужно сделать, это присвоить возвращаемое значение некоторой переменной при вызове функции.

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

Теперь $returnedValue имеет такое же значение, как $target_file было внутри функции.

+0

супер, конечно, - всегда приятно иметь другие смотрят на ваши глупые ошибки. Спасибо. – oldtimer

0
<?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); 
?> 
Смежные вопросы