| | |
readdir help
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
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.
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.
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)
<?php $current_dir = "$DOCUMENT_ROOT"."..//pdfs/"; //Put in second part, the directory - without a leading slash but with a trailing slash! $dir = opendir($current_dir); // Open the sucker echo ("<p><h1>List of available files:</h1></p><hr><br />"); while ($file = readdir($dir)) // while loop { $parts = explode(".", $file); // pull apart the name and dissect by period if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part $extension = end($parts); // set to we can see last file extension if ($extension == "pdf" OR $extension == "PDF") // is extension ext or EXT ? echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />"; // If so, echo it out else do nothing cos it's not what we want } } echo "<hr><br />"; closedir($dir); // Close the directory after we are done ?>
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.
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.
The above assumes your pdf files reside in the 'pdfs' folder directly under html root.
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)
$myfile = "/pdfs/" . $file; 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.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
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....
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.
PHP Syntax (Toggle Plain Text)
<?php $current_dir = "$DOCUMENT_ROOT"."..//pdfs/"; //Put in second part, the directory - without a leading slash but with a trailing slash! $dir = opendir($current_dir); // Open the sucker echo ("<p><h1>List of available files:</h1></p><hr><br />"); while ($file = readdir($dir)) // while loop { $parts = explode(".", $file); // pull apart the name and dissect by period if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part $extension = end($parts); // set to we can see last file extension if ($extension == "pdf" OR $extension == "PDF") // is extension ext or EXT ? $myfile = "/pdfs/" . $file; echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />"; // If so, echo it out else do nothing cos it's not what we want } } echo "<hr><br />"; closedir($dir); // Close the directory after we are done ?>
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.
You didn't change the a link to $myfile:
to
<a href=\"$file\" target=\"_blank\"> $file </a><br /> to
<a href=\"$myfile\" target=\"_blank\"> $file </a><br /> "...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
![]() |
Similar Threads
- Need help with directory displayer code (PHP)
- Visual c++ libraries (C++)
- I DON'T WANT TO USE ''exec Is" command (C)
- writing a script (Shell Scripting)
- Preparing for an interview and need some questions answered (C)
Other Threads in the PHP Forum
- Previous Thread: PHP function called by JQuery performance problem
- Next Thread: how to make an account activation by email
| Thread Tools | Search this Thread |
advanced apache api array beginner binary broken cakephp checkbox class cms code cookies cron curl database date datepart display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google head href htaccess html if...loop image include includingmysecondfileinthechain insert ip javascript job joomla jquery key library limit link login mail menu mlm multiple mysql oop password paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search server sessions smarty sms soap sorting source space sql startup stored syntax system table traffic tutorial update upload url validator variable video web xml youtube zend





