473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $allowed_extensions = array("doc", "pdf");
  4.  
  5. echo php_file_tree($_SERVER['DOCUMENT_ROOT']."/er_test/forms", "[link]", $allowed_extensions);
  6.  
  7. echo php_file_tree("/er_test/", "javascript:alert('You clicked on [link]');");
  8.  
  9. ?>
  10.  
The file link is coming out like this:
d:\Inetpub\noccaschool/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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  
  4.     == PHP FILE TREE ==
  5.  
  6.         Let's call it...oh, say...version 1?
  7.  
  8.     == AUTHOR ==
  9.  
  10.         Cory S.N. LaViska
  11.         http://abeautifulsite.net/
  12.  
  13.     == DOCUMENTATION ==
  14.  
  15.         For documentation and updates, visit http://abeautifulsite.net/notebook.php?article=21
  16.  
  17. */
  18.  
  19.  
  20. function php_file_tree($directory, $return_link, $extensions = array()) {
  21.     // Generates a valid XHTML list of all directories, sub-directories, and files in $directory
  22.     // Remove trailing slash
  23.     if( substr($directory, -1) == "/" ) $directory = substr($directory, 0, strlen($directory) - 1);
  24.     $code .= php_file_tree_dir($directory, $return_link, $extensions);
  25.     return $code;
  26. }
  27.  
  28. function php_file_tree_dir($directory, $return_link, $extensions = array(), $first_call = true) {
  29.     // Recursive function called by php_file_tree() to list directories/files
  30.  
  31.     // Get and sort directories/files
  32.     if( function_exists("scandir") ) $file = scandir($directory); else $file = php4_scandir($directory);
  33.     natcasesort($file);
  34.     // Make directories first
  35.     $files = $dirs = array();
  36.     foreach($file as $this_file) {
  37.         if( is_dir("$directory/$this_file" ) ) $dirs[] = $this_file; else $files[] = $this_file;
  38.     }
  39.     $file = array_merge($dirs, $files);
  40.  
  41.     // Filter unwanted extensions
  42.     if( !empty($extensions) ) {
  43.         foreach( array_keys($file) as $key ) {
  44.             if( !is_dir("$directory/$file[$key]") ) {
  45.                 $ext = substr($file[$key], strrpos($file[$key], ".") + 1); 
  46.                 if( !in_array($ext, $extensions) ) unset($file[$key]);
  47.             }
  48.         }
  49.     }
  50.  
  51.     if( count($file) > 2 ) { // Use 2 instead of 0 to account for . and .. "directories"
  52.         $php_file_tree = "<ul";
  53.         if( $first_call ) { $php_file_tree .= " class=\"php-file-tree\""; $first_call = false; }
  54.         $php_file_tree .= ">";
  55.         foreach( $file as $this_file ) {
  56.             if( $this_file != "." && $this_file != ".." ) {
  57.                 if( is_dir("$directory/$this_file") ) {
  58.                     // Directory
  59.                     $php_file_tree .= "<li class=\"pft-directory\"><a href=\"#\">" . htmlspecialchars($this_file) . "</a>";
  60.                     $php_file_tree .= php_file_tree_dir("$directory/$this_file", $return_link ,$extensions, false);
  61.                     $php_file_tree .= "</li>";
  62.                 } else {
  63.                     // File
  64.                     // Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers)
  65.                     $ext = "ext-" . substr($this_file, strrpos($this_file, ".") + 1); 
  66.                     $link = str_replace("[link]", "$directory/" . urlencode($this_file), $return_link);
  67.                     $php_file_tree .= "<li class=\"pft-file " . strtolower($ext) . "\"><a href=\"$link\">" . htmlspecialchars($this_file) . "</a></li>";
  68.                 }
  69.             }
  70.         }
  71.         $php_file_tree .= "</ul>";
  72.     }
  73.     return $php_file_tree;
  74. }
  75.  
  76. // For PHP4 compatibility
  77. function php4_scandir($dir) {
  78.     $dh  = opendir($dir);
  79.     while( false !== ($filename = readdir($dh)) ) {
  80.         $files[] = $filename;
  81.     }
  82.     sort($files);
  83.     return($files);
  84. }
  85.  
Thanks,
Carlos
Aug 25 '10 #1
3 2428
TheServant
1,168 Expert 1GB
To manually set the path is as simple as replacing:
Expand|Select|Wrap|Line Numbers
  1. $_SERVER['DOCUMENT_ROOT']."/er_test/forms
with:
Expand|Select|Wrap|Line Numbers
  1. path/to/file/
I'm still not sure what exactly you want?
Aug 27 '10 #2
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:
Expand|Select|Wrap|Line Numbers
  1. d:\Inetpub\noccaschool/er_test/forms/form_name.pdf
instead of:
Expand|Select|Wrap|Line Numbers
  1. http://www.nocca.com/er_test/forms/form_name.pdf
Does that help?

Thanks
Aug 27 '10 #3
TheServant
1,168 Expert 1GB
Hmmmm... I have never tried to do that but it seems simple.

When I use $_SERVER['DOCUMENT_ROOT'] I get:
Expand|Select|Wrap|Line Numbers
  1. /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:
Expand|Select|Wrap|Line Numbers
  1. echo "http://".$_SERVER['SERVER_NAME']."/er_test/forms";
Aug 27 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: David Webb | last post by:
The problem started when the Working Folder for a project was somehow set to the folder of another project. I set the correct working folder in VSS and deleted the .vbproj files that had been...
3
by: VK | last post by:
On Windows platforms path separator "\" collides with the script escape sign "\" Obvious and old way to prevent it is to double backslashes: "\\" But I'm curious if there is a reliable way to...
3
by: Mark | last post by:
Hello all - I'm at wits end with trying to figure out why I'm having this problem since I followed the research, that I've dug up, to a "T". I'm trying to display a JPEG image from a path...
0
by: Rob | last post by:
Hope you can help me on this one. The scenario is this - i have integrated VS.NET with VSS and am now trying to set-up an automated build script which does the following: 1) Labels most recent...
10
by: darrel | last post by:
I have an input type="file" field that I am using to accept a file upload. This works, but I'm having problems with the filename property. In firefox, this: MyInputField.postedfile.filename ...
3
by: Vibhu | last post by:
Hello All, I have a input box on the HTML page with the type set to file. What I want is that when the value changes in the file textbox, it should give me the full file path. I have even tried...
10
by: Eric | last post by:
Hello, I have some server side includes on a Classic asp page that look something like: <!-- #include virtual="/includes/file1.asp"--> <!-- #include virtual="/includes/file2.asp" --> <!--...
5
by: Tawreq | last post by:
Hi All, I have quite a unique issue - I would be very grateful if someone could help out with this challenge.... I have two columns, Column A contains and acronym, and Column B contains a...
2
by: mpalomas | last post by:
Hi C++ folks, I have trouble to open files whose path contains non-ascii characters with std::ifstream. For instance let's say i just have a file which has Japanese characters either in the...
10
by: Raheem | last post by:
Hello, I built a development version of a live website on my hosted account. However the development version is having problems with finding include files. After troubleshooting I was able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.