我在我的PHP文件中有以下代碼 - 初始化$ uploaded_files變量,然後調用getDirectory(也在下面列出)。PHP串聯,字符串長度= 0
現在,如果我做一個vardump($ uploaded_files)我看到我的變量的內容,但由於某些原因,當我打電話<?php echo $uploaded_files; ?>
在我的HTML文件,我得到一個消息,說明「沒有找到文件」 - 我在做一些不正確?
有人可以協助嗎?謝謝。
/** LIST UPLOADED FILES **/
$uploaded_files = "";
getDirectory(Settings::$uploadFolder);
// Check if the uploaded_files variable is empty
if(strlen($uploaded_files) == 0)
{
$uploaded_files = "<li><em>No files found</em></li>";
}
getDirectory功能:
function getDirectory($path = '.', $level = 0)
{
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$ignore = array('cgi-bin', '.', '..');
// Open the directory to the handle $dh
$dh = @opendir($path);
// Loop through the directory
while(false !== ($file = readdir($dh))){
// Check that this file is not to be ignored
if(!in_array($file, $ignore)){
// Its a directory, so we need to keep reading down...
if(is_dir("$path/$file")){
// We are now inside a directory
// Re-call this same function but on a new directory.
// this is what makes function recursive.
getDirectory("$path/$file", ($level+1));
}
else {
// Just print out the filename
// echo "$file<br />";
$singleSlashPath = str_replace("uploads//", "uploads/", $path);
if ($path == "uploads/") {
$filename = "$path$file";
}
else $filename = "$singleSlashPath/$file";
$parts = explode("_", $file);
$size = formatBytes(filesize($filename));
$added = date("m/d/Y", $parts[0]);
$origName = $parts[1];
$filetype = getFileType(substr($file, strlen($file) - 4));
$uploaded_files .= "<li class=\"$filetype\"><a href=\"$filename\">$origName</a> $size - $added</li>\n";
// var_dump($uploaded_files);
}
}
}
// Close the directory handle
closedir($dh);
}
$ uploaded_files未在您的函數中聲明爲全局。 – crush
噢,我做了 - 完全錯過了,謝謝你的幫助。只要Stackoverflow讓我接受你的答案。 – mattdonders