Я пытаюсь получить имена файлов из папки внутри папки (в папке ../merchant_assets/
есть папка с именем 1
, и я пытаюсь получить все файлы имен внутри этой папки)
Приведенный ниже код работает нормально, однако, когда я назначил свой массив, состоящий из результата $temp
, переменной stdClass()
$container
, он становится пустым, когда я пытаюсь распечатать массив вне функцииprint_r($container->screenshots)
но это работает, если я распечатаю его внутри функции
<?php
include 'config.php';
$url = "http://" . $_SERVER['HTTP_HOST'] . '/merchant_assets/';
$target = '../merchant_assets';
$merchant_id = 1;
$container = new stdClass();
$folder = '';
// Call the function
dir_contents_recursive($target);
// Get all screenshots images from merchant_assets folder based on merchant_id
function dir_contents_recursive($dir) {
// open handler for the directory
global $container;
$iter = new DirectoryIterator($dir);
$temp = Array();
foreach( $iter as $item ) {
// make sure you don't try to access the current dir or the parent
if ($item != '.' && $item != '..') {
if( $item->isDir() ) {
// call the function on the folder
global $merchant_id, $folder;
if($merchant_id == $item->getFilename()) {
$folder = $item->getFilename();
dir_contents_recursive("$dir/$item");
}else
continue;
} else {
// print files
global $url, $folder;
$current_index = count($temp);
$temp[$current_index] = $url . $folder . '/' . $item->getFilename();
}
}
}
$container->screenshots = $temp;
print_r($container->screenshots); // It shows the results
}
// Handle response
$response = $container;
print_r($container->screenshots); // No results???
$response_json = json_encode($response);
echo $response_json;
?>
Я ожидал, что результат будет [Я намеренно изменил значение каждого индекса на item
]
Array (
[0] => item
[1] => item
[2] => item
[3] => item