943,648 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 735
  • PHP RSS
May 14th, 2009
0

readdir help

Expand Post »
Hey all,

I found this script that i'm using to read a directory, show all the pdf files in it, then be able to click on one of the pdf files displayed and open the pdf. So far i can show the pdfs in the directory, but when I click on one, i get an error page. Can someone take a look at this and tell me what I'm doing wrong. I'm assuming that my browser is not seeing it as a .pdf file and doesn't know how to open it.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $current_dir = "$DOCUMENT_ROOT"."..//pdfs/"; //Put in second part, the directory - without a leading slash but with a trailing slash!
  3. $dir = opendir($current_dir); // Open the sucker
  4.  
  5. echo ("<p><h1>List of available files:</h1></p><hr><br />");
  6. while ($file = readdir($dir)) // while loop
  7. {
  8. $parts = explode(".", $file); // pull apart the name and dissect by period
  9. if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part
  10. $extension = end($parts); // set to we can see last file extension
  11. if ($extension == "pdf" OR $extension == "PDF") // is extension ext or EXT ?
  12. echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />"; // If so, echo it out else do nothing cos it's not what we want
  13. }
  14. }
  15. echo "<hr><br />";
  16. closedir($dir); // Close the directory after we are done
  17. ?>

Right now this is looking into a directory on my wwwroot folder because I can not get it to see any directories outside of my wwwroot folder. I'd like it to see files in a directory on a shared fileserver on my network if anyone can help with that, haven't been able to get it to work and my knowledge is not what it should be yet.

Thanks for any help.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
May 14th, 2009
0

Re: readdir help

I don't think that you can link to a file above the html root (I may be wrong). A 'forbidden?' error or something similar should appear.

From what I can see, I think your link is referring to just the filename (so it will search the current page file folder for the pdf) - you need to add an absolute path to this,
e.g.
PHP Syntax (Toggle Plain Text)
  1. $myfile = "/pdfs/" . $file;
  2. echo "<a href=\"{$myfile}\" ...>{$file}</a>";

The above assumes your pdf files reside in the 'pdfs' folder directly under html root.
Last edited by ardav; May 14th, 2009 at 4:21 pm.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,671 posts
since Oct 2006
May 14th, 2009
0

Re: readdir help

ok, here is what I found out. The above php file i'm using is in a folder under wwwroot/scanner/readdir2.php. I'm trying to get this file to read the contents of a folder that is at wwwroot/pdfs/. I made the changes you suggested....
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $current_dir = "$DOCUMENT_ROOT"."..//pdfs/"; //Put in second part, the directory - without a leading slash but with a trailing slash!
  3. $dir = opendir($current_dir); // Open the sucker
  4.  
  5. echo ("<p><h1>List of available files:</h1></p><hr><br />");
  6. while ($file = readdir($dir)) // while loop
  7. {
  8. $parts = explode(".", $file); // pull apart the name and dissect by period
  9. if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part
  10. $extension = end($parts); // set to we can see last file extension
  11. if ($extension == "pdf" OR $extension == "PDF") // is extension ext or EXT ?
  12. $myfile = "/pdfs/" . $file;
  13. echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />"; // If so, echo it out else do nothing cos it's not what we want
  14. }
  15. }
  16. echo "<hr><br />";
  17. closedir($dir); // Close the directory after we are done
  18. ?>

And it is pulling the contents of the pdfs folder, but will not open it unless the pdf file i'm showing is in the wwwroot/scanner folder. So its reading the directory wwwroot/pdfs, but trying to open the file in the wwwroot/scanner directory, where my php files are. Any idea as to why its doing this? Is $DOCUMENT_ROOT pointing to the files my php files are in or to the wwwroot folder? How could I see this?

Thanks for your help.
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
May 14th, 2009
0

Re: readdir help

You didn't change the a link to $myfile:

<a href=\"$file\" target=\"_blank\"> $file </a><br />
to

<a href=\"$myfile\" target=\"_blank\"> $file </a><br />
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,671 posts
since Oct 2006
May 14th, 2009
0

Re: readdir help

Click to Expand / Collapse  Quote originally posted by ardav ...
You didn't change the a link to $myfile:

<a href=\"$file\" target=\"_blank\"> $file </a><br />
to

<a href=\"$myfile\" target=\"_blank\"> $file </a><br />
Ahh, good catch, you are right, works much better now. Thank you for the help. No to work on the rest. Much appreciated.
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP function called by JQuery performance problem
Next Thread in PHP Forum Timeline: how to make an account activation by email





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC