Trying to resolve file path issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Carlos Avrard
    New Member
    • Aug 2010
    • 2

    Trying to resolve file path issue

    Working with a simple directory/file tree display solution (http://abeautifulsite. net/blog/2007/06/php-file-tree/)

    I'm able to get the page to display properly, javascript works just fine as well, but I can't seem to get the correct file path on the file links.

    here's the code:

    Code:
    <?php
    		
    $allowed_extensions = array("doc", "pdf");
    
    echo php_file_tree($_SERVER['DOCUMENT_ROOT']."/er_test/forms", "[link]", $allowed_extensions);
    	
    echo php_file_tree("/er_test/", "javascript:alert('You clicked on [link]');");
    		
    ?>
    The file link is coming out like this:
    d:\Inetpub\nocc aschool/er_test/forms/Student%20Forms/Student+Check-Out+Procedures. doc

    I'm thinking that it could be something in how the website is structured, is there a work around or a manual way to set the path?

    If needed here's the php_file_tree code:
    Code:
    <?php
    /*
    	
    	== PHP FILE TREE ==
    	
    		Let's call it...oh, say...version 1?
    	
    	== AUTHOR ==
    	
    		Cory S.N. LaViska
    		http://abeautifulsite.net/
    		
    	== DOCUMENTATION ==
    	
    		For documentation and updates, visit http://abeautifulsite.net/notebook.php?article=21
    		
    */
    
    
    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=\"php-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=\"pft-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=\"pft-file " . strtolower($ext) . "\"><a href=\"$link\">" . 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);
    }
    Thanks,
    Carlos
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    To manually set the path is as simple as replacing:
    Code:
    $_SERVER['DOCUMENT_ROOT']."/er_test/forms
    with:
    Code:
    path/to/file/
    I'm still not sure what exactly you want?

    Comment

    • Carlos Avrard
      New Member
      • Aug 2010
      • 2

      #3
      First of all, thanks for the reply.

      The issue I'm having is that the link to download the files that are displayed through the file tree can't be downloaded because the url is malformed.

      It shows up for example as:
      Code:
      d:\Inetpub\noccaschool/er_test/forms/form_name.pdf
      instead of:
      Code:
      http://www.nocca.com/er_test/forms/form_name.pdf
      Does that help?

      Thanks

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Hmmmm... I have never tried to do that but it seems simple.

        When I use $_SERVER['DOCUMENT_ROOT'] I get:
        Code:
        /home/username/public_html/
        However mine is remotely hosted, so I image that that is the difference.
        $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] both return my domain name with the domain extension. What do those return on yours?

        If on the small chance that they do return your domain, then you can use something like:
        Code:
        echo "http://".$_SERVER['SERVER_NAME']."/er_test/forms";

        Comment

        Working...