Seapoeschl Posted June 6, 2012 Share Posted June 6, 2012 Hello, I am currently working on an intranet website for the company I work at. I know more about Graphic Design then coding, so you can say I'm new to php code. The site is going to be used to let users log on and submit/view pdf files that are stored on the site. I have everything else created for the site(data form to build pdf, upload form, etc.), but I can't seem to figure out how to let the users view the files. I want to create a page where they can use a search bar to find the certain file that is stored in the files folder. I have tried using the dir() and scandir() commands but they just list the files and are not able to open them. I don't want to hard code the .PDF files on a page since they will be constantly changing and being added. Is there a way to have these files be displayed (like a link) and have a search bar to be able to search through them to find the correct one needed? Thanks, Seattle Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 6, 2012 Share Posted June 6, 2012 When you list the files, surround the name in a link to the file, using the <a href=""> tag in HTML. Post your code if you're stuck. Quote Link to comment Share on other sites More sharing options...
Seapoeschl Posted June 6, 2012 Author Share Posted June 6, 2012 This is the current code that I have for the display page. <?php //Open images directory $dir = dir("TestUpload"); //List files in images directory while (($file = $dir->read()) !== true) { echo "filename: " . $file . "<br />"; } $dir->close(); ?> I attached a photo of what shows up in the browser. I want to be able to be able to click on the file name and have it open. Also have a search bar that will look through that Folder and find the file that matches the search. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 7, 2012 Share Posted June 7, 2012 So when you echo the file name, wrap it in an html link. Do you know how to make a link in HTML? Quote Link to comment Share on other sites More sharing options...
Seapoeschl Posted June 7, 2012 Author Share Posted June 7, 2012 I think I am a little bit confused about what you mean by wrapping with an html link. I have done it before where I would just hard code each file to the page by doing something like this: <a href="pdf/Newsletter_Spring_2012.pdf" target="_blank">Pioneer Xpress Newsletter - Spring 2012</a> How would I type the code so each file will have a link without having to hand code a new link every time a file is added? Quote Link to comment Share on other sites More sharing options...
TOA Posted June 7, 2012 Share Posted June 7, 2012 Unless I'm missing something.. echo "filename: <a href='$file'>" . $file . "</a><br />"; Quote Link to comment Share on other sites More sharing options...
Seapoeschl Posted June 7, 2012 Author Share Posted June 7, 2012 Unless I'm missing something.. echo "filename: <a href='$file'>" . $file . "</a><br />"; I tried this method, but I is not looking in the directory that it is pulling the file name from. The link is sending you to the same folder that the PHP file is stored, and looking for that file in there. I have these .PDF stored in a secure folder else where. Quote Link to comment Share on other sites More sharing options...
TOA Posted June 7, 2012 Share Posted June 7, 2012 Then just add the path to the right directory in the href, before $file <a href='path/to/dir/$file'>$file</a> Quote Link to comment Share on other sites More sharing options...
Seapoeschl Posted June 7, 2012 Author Share Posted June 7, 2012 I was able to get it to work when the folder is in the root folder of the PHP file. I have the folder that stores the .PDF files located outside the root folder. I will display the names of the files, but when I link them with the same path it wont open those files. Here is my code and paths(I tried with both / and \). <?php //Open images directory $dir = dir("D:\TESTUP"); //List files in images directory while (($file = $dir->read()) !== false) { echo "filename: <a href='D:\TESTUP\$file'>$file</a><br />"; } $dir->close(); ?> PHP files are located in: D:\xampp\htdocs\sitea .PDF files are located in: D:\TESTUP Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.