Autor Zpráva
MartinXYZ
Profil *
Ahoj,

moc prosím o pomoc, co mám na tomto skriptu špatně. Skript slouží k sestavení "stromu" souborů a složek.
Bohužel mi to ale vyhazuje tyto chyby:

Notice: Undefined variable: php_file_tree on line 56
return $php_file_tree;

Notice: Undefined variable: php_file_tree on line 7
$code .= php_file_tree_dir($directory, $return_link, $extensions);

Prosím, pomůže mi někdo odhalit chybu v daných řádcích?

MOC děkuju,
M.


<?php

function php_file_tree($directory, $return_link, $extensions = array()) {
    // Generates a valid XHTML list of all directories, sub-directories, and files in $directory
    // Remove trailing slash
    if( substr($directory, -1) == "/" ) $directory = substr($directory, 0, strlen($directory) - 1);
    $code .= php_file_tree_dir($directory, $return_link, $extensions);
    return $code;
}

function php_file_tree_dir($directory, $return_link, $extensions = array(), $first_call = true) {
    // Recursive function called by php_file_tree() to list directories/files
    
    // Get and sort directories/files
    if( function_exists("scandir") ) $file = scandir($directory); else $file = php4_scandir($directory);
    natcasesort($file);
    // Make directories first
    $files = $dirs = array();
    foreach($file as $this_file) {
        if( is_dir("$directory/$this_file" ) ) $dirs[] = $this_file; else $files[] = $this_file;
    }
    $file = array_merge($dirs, $files);
    
    // Filter unwanted extensions
    if( !empty($extensions) ) {
        foreach( array_keys($file) as $key ) {
            if( !is_dir("$directory/$file[$key]") ) {
                $ext = substr($file[$key], strrpos($file[$key], ".") + 1); 
                if( !in_array($ext, $extensions) ) unset($file[$key]);
            }
        }
    }
    
    if( count($file) > 2 ) { // Use 2 instead of 0 to account for . and .. "directories"
        $php_file_tree = "<ul";
        if( $first_call ) { $php_file_tree .= " class=\"file-tree\""; $first_call = false; }
        $php_file_tree .= ">";
        foreach( $file as $this_file ) {
            if( $this_file != "." && $this_file != ".." ) {
                if( is_dir("$directory/$this_file") ) {
                    // Directory
                    $php_file_tree .= "<li class=\"ft-directory\"><a href=\"#\">" . htmlspecialchars($this_file) . "</a>";
                    $php_file_tree .= php_file_tree_dir("$directory/$this_file", $return_link ,$extensions, false);
                    $php_file_tree .= "</li>";
                } else {
                    // File
                    // Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers)
                    $ext = "ext-" . substr($this_file, strrpos($this_file, ".") + 1); 
                    $link = str_replace("[link]", "$directory/" . urlencode($this_file), $return_link);
                    $php_file_tree .= "<li class=\"ft-file " . strtolower($ext) . "\"><a href=\"$link\" target=\"_blank\">" . htmlspecialchars($this_file) . "</a></li>";
                }
            }
        }
        $php_file_tree .= "</ul>";
    }
    return $php_file_tree;
}

// For PHP4 compatibility
function php4_scandir($dir) {
    $dh  = opendir($dir);
    while( false !== ($filename = readdir($dh)) ) {
        $files[] = $filename;
    }
    sort($files);
    return($files);
}

?>
Taps
Profil
MartinXYZ:
zkus
<?php
 
function php_file_tree($directory, $return_link, $extensions = array()) {
    // Generates a valid XHTML list of all directories, sub-directories, and files in $directory
    // Remove trailing slash
    $code = '';
    if( substr($directory, -1) == "/" ) $directory = substr($directory, 0, strlen($directory) - 1);
    if(isset($code)){
    $code .= php_file_tree_dir($directory, $return_link, $extensions);
    }
    return $code;
}
 
function php_file_tree_dir($directory, $return_link, $extensions = array(), $first_call = true) {
    // Recursive function called by php_file_tree() to list directories/files
    
    // Get and sort directories/files
    if( function_exists("scandir") ) $file = scandir($directory); else $file = php4_scandir($directory);
    natcasesort($file);
    // Make directories first
    $files = $dirs = array();
    foreach($file as $this_file) {
        if( is_dir("$directory/$this_file" ) ) $dirs[] = $this_file; else $files[] = $this_file;
    }
    $file = array_merge($dirs, $files);
    
    // Filter unwanted extensions
    if( !empty($extensions) ) {
        foreach( array_keys($file) as $key ) {
            if( !is_dir("$directory/$file[$key]") ) {
                $ext = substr($file[$key], strrpos($file[$key], ".") + 1); 
                if( !in_array($ext, $extensions) ) unset($file[$key]);
            }
        }
    }
    
    if( count($file) > 2 ) { // Use 2 instead of 0 to account for . and .. "directories"
        $php_file_tree = "<ul";
        if( $first_call ) { $php_file_tree .= " class=\"file-tree\""; $first_call = false; }
        $php_file_tree .= ">";
        foreach( $file as $this_file ) {
            if( $this_file != "." && $this_file != ".." ) {
                if( is_dir("$directory/$this_file") ) {
                    // Directory
                    $php_file_tree .= "<li class=\"ft-directory\"><a href=\"#\">" . htmlspecialchars($this_file) . "</a>";
                    $php_file_tree .= php_file_tree_dir("$directory/$this_file", $return_link ,$extensions, false);
                    $php_file_tree .= "</li>";
                } else {
                    // File
                    // Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers)
                    $ext = "ext-" . substr($this_file, strrpos($this_file, ".") + 1); 
                    $link = str_replace("[link]", "$directory/" . urlencode($this_file), $return_link);
                    $php_file_tree .= "<li class=\"ft-file " . strtolower($ext) . "\"><a href=\"$link\" target=\"_blank\">" . htmlspecialchars($this_file) . "</a></li>";
                }
            }
        }
        $php_file_tree .= "</ul>";
    }
    if(isset($php_file_tree)){
    return $php_file_tree;
    }
}
 
// For PHP4 compatibility
function php4_scandir($dir) {
    $dh  = opendir($dir);
    while( false !== ($filename = readdir($dh)) ) {
        $files[] = $filename;
    }
    sort($files);
    return($files);
}
 
?>
Lonanek
Profil
Dle mého názoru stačí pouze přidat ř.6 z kódu Taps.
Ostatní ponechte jak máte. Tedy:
<?php
 
function php_file_tree($directory, $return_link, $extensions = array()) {
    // Generates a valid XHTML list of all directories, sub-directories, and files in $directory
    // Remove trailing slash
    $code = '';
    if( substr($directory, -1) == "/" ) $directory = substr($directory, 0, strlen($directory) - 1);
    $code = php_file_tree_dir($directory, $return_link, $extensions);
    return $code;
}

Důvod chyby:
pokud podmínka není splněna, neexistuje proměnná $code a return nemá co vrátit (err ř. 11), protože je funkce volána z ř. 56, tak tam nastane chyba protože se nic nevrátí..
Navíc přičítat řetězec k něčemu co neexistuje vrací také chybu $code .=
Alphard
Profil
Ta lokální proměnná nebude existovat nikdy, proč tam nedáte normální přiřazení? Resp. hned return? $code tam vůbec není potřeba.
A ta podmínka by šla nahradit pomocí rtrim, celou funkci bych zkrátil na jeden řádek.
MartinXYZ
Profil *
Ahoj, děkuju za reakce.

Bohužel řešení od Tapse a Lonanka nefunguje :-(.

Zkusil jsem tedy řešení dle Alpharda (snad jsem to dobře pochopil? doufám...) - dal jsem ze 7. řádku pryč $code a místo něj tam dal rovnou return.

Chyba ze 7. řádku zmizela, ale na 56. se pořád ukazuje. A to už fakt nevím, jak opravit :-(.

Takto jsem opravil 7. řádek
<?php
 
function php_file_tree($directory, $return_link, $extensions = array()) {
    // Generates a valid XHTML list of all directories, sub-directories, and files in $directory
    // Remove trailing slash
    if( substr($directory, -1) == "/" ) $directory = substr($directory, 0, strlen($directory) - 1);
    return php_file_tree_dir($directory, $return_link, $extensions);

}
 
function php_file_tree_dir($directory, $return_link, $extensions = array(), $first_call = true) {
    // Recursive function called by php_file_tree() to list directories/files
    
    // Get and sort directories/files
    if( function_exists("scandir") ) $file = scandir($directory); else $file = php4_scandir($directory);
    natcasesort($file);
    // Make directories first
    $files = $dirs = array();
    foreach($file as $this_file) {
        if( is_dir("$directory/$this_file" ) ) $dirs[] = $this_file; else $files[] = $this_file;
    }
    $file = array_merge($dirs, $files);
    
    // Filter unwanted extensions
    if( !empty($extensions) ) {
        foreach( array_keys($file) as $key ) {
            if( !is_dir("$directory/$file[$key]") ) {
                $ext = substr($file[$key], strrpos($file[$key], ".") + 1); 
                if( !in_array($ext, $extensions) ) unset($file[$key]);
            }
        }
    }
    
    if( count($file) > 2 ) { // Use 2 instead of 0 to account for . and .. "directories"
        $php_file_tree = "<ul";
        if( $first_call ) { $php_file_tree .= " class=\"file-tree\""; $first_call = false; }
        $php_file_tree .= ">";
        foreach( $file as $this_file ) {
            if( $this_file != "." && $this_file != ".." ) {
                if( is_dir("$directory/$this_file") ) {
                    // Directory
                    $php_file_tree .= "<li class=\"ft-directory\"><a href=\"#\">" . htmlspecialchars($this_file) . "</a>";
                    $php_file_tree .= php_file_tree_dir("$directory/$this_file", $return_link ,$extensions, false);
                    $php_file_tree .= "</li>";
                } else {
                    // File
                    // Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers)
                    $ext = "ext-" . substr($this_file, strrpos($this_file, ".") + 1); 
                    $link = str_replace("[link]", "$directory/" . urlencode($this_file), $return_link);
                    $php_file_tree .= "<li class=\"ft-file " . strtolower($ext) . "\"><a href=\"$link\" target=\"_blank\">" . htmlspecialchars($this_file) . "</a></li>";
                }
            }
        }
        $php_file_tree .= "</ul>";
    }
    return $php_file_tree;
}
 
// For PHP4 compatibility
function php4_scandir($dir) {
    $dh  = opendir($dir);
    while( false !== ($filename = readdir($dh)) ) {
        $files[] = $filename;
    }
    sort($files);
    return($files);
}
 
?>
Lonanek
Profil
Zkuste na ř. 12 vložit:
$php_file_tree = "";
MartinXYZ
Profil *
Ooooo, děkuju moc! :-)
Chyba na 56. řádku je nyní pryč, paráda :-).

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0