0

PHPファイルに次のコードがあります-$uploaded_files変数を初期化してから、getDirectoryを呼び出します(これも以下にリストされています)。

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 ); 
} 
4

2 に答える 2

4

次のいずれかを追加する必要があります。

global $uploaded_files;

getDirectory関数の上部、または

function getDirectory( &$uploaded_files, $path = '.', $level = 0 )

参照して渡してください。

$uploaded_filesをgetDirectoryの戻り値にすることもできます。

グローバルとセキュリティの詳細:http://php.net/manual/en/security.globals.php

于 2013-01-11T21:11:02.110 に答える
2

PHPはスコープについて何も知りません。

関数の本体内から変数を宣言すると、その関数のスコープ外では使用できなくなります。

例えば:

function add(){
    $var = 'test';
}

var_dump($var); // undefined variable $var

これはまさに、変数にアクセスしようとしたときに発生する問題です$uploaded_files

于 2013-01-11T21:13:29.750 に答える