PHP连接,字符串长度=0
我的PHP文件中有以下代码-初始化$upload_files变量,然后调用getDirectory(也在下面列出) 现在,如果我执行vardump($uploaded_files),我会看到变量的内容,但由于某种原因,当我在HTML文件中调用PHP连接,字符串长度=0,php,concatenation,Php,Concatenation,我的PHP文件中有以下代码-初始化$upload_files变量,然后调用getDirectory(也在下面列出) 现在,如果我执行vardump($uploaded_files),我会看到变量的内容,但由于某种原因,当我在HTML文件中调用时,我会收到一条消息,说明“找不到文件”-我是否做了一些不正确的事情 有人能帮忙吗?多谢各位 /** LIST UPLOADED FILES **/ $uploaded_files = ""; getDirectory( Settings::$upload
时,我会收到一条消息,说明“找不到文件”-我是否做了一些不正确的事情
有人能帮忙吗?多谢各位
/** 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>";
}
/**列出上载的文件**/
$uploaded_files=“”;
getDirectory(设置::$uploadFolder);
//检查上传的_files变量是否为空
if(strlen($upload_files)==0)
{
$uploaded_files=“未找到文件” ”;
}
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 );
}
函数getDirectory($path='。,$level=0)
{
//列出输出时要忽略的目录。许多主机
//将拒绝PHP访问cgi bin。
$ignore=array('cgi bin','.','..);
//打开句柄$dh的目录
$dh=@opendir($path);
//循环浏览目录
while(false!==($file=readdir($dh)){
//检查此文件是否不被忽略
如果(!in_数组($file,$ignore)){
//这是一个目录,所以我们需要继续往下读。。。
如果(is_dir(“$path/$file”){
//我们现在在一个目录中
//重新调用相同的函数,但在新目录上。
//这就是函数递归的原因。
getDirectory($path/$file),($level+1));
}
否则{
//只需打印出文件名
//回显“$file”; $singleSlashPath=str_replace(“uploads/”、“uploads/”、$path); 如果($path==“uploads/”){ $filename=“$path$file”; } else$filename=“$singleSlashPath/$file”; $parts=explode(“\”,$file); $size=formatBytes(filesize($filename)); $added=日期(“m/d/Y”,$parts[0]); $origName=$parts[1]; $filetype=getFileType(substr($file,strlen($file)-4); $uploaded\U文件。=“
您需要添加:
global $uploaded_files;
在getDirectory函数的顶部,或
function getDirectory( &$uploaded_files, $path = '.', $level = 0 )
通过引用传递它
您还可以将$uploaded\u files作为getDirectory的返回值
有关全局和安全性的更多阅读:您需要添加:
global $uploaded_files;
在getDirectory函数的顶部,或
function getDirectory( &$uploaded_files, $path = '.', $level = 0 )
通过引用传递它
您还可以将$uploaded\u files作为getDirectory的返回值
更多关于全局和安全性的阅读:PHP对范围一无所知 当您在函数体中声明变量时,该变量在所述函数范围之外将不可用 例如:
function add(){
$var = 'test';
}
var_dump($var); // undefined variable $var
这正是您在尝试访问变量
$uploaded\u files
时遇到的问题,PHP对范围一无所知
当您在函数体中声明变量时,该变量在所述函数范围之外将不可用
例如:
function add(){
$var = 'test';
}
var_dump($var); // undefined variable $var
这正是您在尝试访问变量
$uploaded\u files
$uploaded\u files未在函数中声明为全局变量时遇到的问题。哦,我确实这样做了-完全错过了,谢谢您的帮助。只要Stackoverflow允许,我将尽快接受您的回答。$uploaded_文件在您的函数中未声明为全局文件。哦,我做了-完全错过了,谢谢您的帮助。我会尽快接受你的回答。