在Stackoverflow上找到了2个线程,hovever这些是关于如何返回一个数组.我可以在函数内部生成一个数组并且它具有正确的内容,但是当在函数外部执行$ target_file的var_dumping时它是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].'
'; copy($src_filename[$i],$target_file[$i]); $i++; } return($target_file); }
我将文件重命名并放在我服务器上的正确文件夹中,唯一的问题是在此函数之后访问$ target_file数组.
$target_file
仅存在于函数内部.是的,这是范围的事情.
您正在返回此变量的值,但不返回变量本身.
您所要做的就是在调用函数时将返回值赋给某个变量.
$returnedValue = copy_files3($requested, $src_path, $send_path);
现在,$returnedValue
具有与$target_file
函数内部相同的值.