以下函數返回空字符串。有人可以建議下面的代碼有什麼問題嗎?我正在對此函數進行AJAX調用。對Wordpress函數的Ajax調用返回空數組
function LoadImagesForMenus() {
$filenameArray = [];
$validextensions = ["gif", "jpg", "jpeg", "png"];
$handle = opendir(get_template_directory().'/analyzer-images/');
while ($file = readdir($handle)) {
if ($file !== '.' && $file !== '..' && !empty($file)) {
$filenameArray[] = $file;
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (in_array($ext, $validextensions)) {
$fileArray = [];
$fileArray["fileName"] = "http://xn--ernhrungsberaterzrich-71b08c.ch/wp-content/themes/abundance/analyzer-images/$file";
list($width, $height) = getimagesize("/home/httpd/vhosts/xn--ernhrungsberaterzrich-71b08c.ch/httpdocs/wp-content/themes/abundance/analyzer-images/$file");
$fileArray["width"] = $width;
$fileArray["height"] = $height;
if ($width !== null && $height !== null) {
array_push($filenameArray, $fileArray);
}
}
}
}
echo json_encode($filenameArray);
exit(0);
}
感謝
你有什麼試圖調試它?你確認文件存在,你的路徑是否正確,文件是否被成功讀取,$ filenameArray在返回等之前是否有正確的值? – FluffyKitten
文件夾中存在文件。 Wordpress沒有在上面的代碼中記錄任何錯誤。移動語句echo json_encode($ filenameArray);在while循環中返回包含圖像信息的數組。 – user1404963
所以問題是數組得到清除不知何故,是嗎?在while循環中的位置是數組打印 - 它是否在'if($ width!== null && $ height!== null)之前或之後「 '? – FluffyKitten