find a pdf file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dwhphp
Forum Newbie
Posts: 2
Joined: Sat Jan 28, 2012 1:10 pm

find a pdf file

Post by dwhphp »

I am trying to come up with a php script that will find a pdf file in a folder and display the results. This is in my site only. I need also to be able to open the pdf. I have looked at swish-e, but that is too complicated for me and a lot more than I need. I don't need to search in mysql.
The file are in the root/files at the site. The reason for the search, there will be 100's of pdf files.
Any help would will be appreciated. I am a newbie.
dwhphp
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: find a pdf file

Post by califdon »

So you're looking for a pdf with a specific filename? If you know the filename and the directory, you can simply use file_exists() (http://us.php.net/manual/en/function.file-exists.php) to determine if it exists. If you want to display all pdf files in a directory, you can use readdir() (http://php.net/manual/en/function.readdir.php). I'm not quite sure I know what you are trying to do.

The simplest way to display a pdf file is to link to it. All browsers should support rendering of pdf files, typically through a plug-in, which most browsers already have installed.
dwhphp
Forum Newbie
Posts: 2
Joined: Sat Jan 28, 2012 1:10 pm

Re: find a pdf file

Post by dwhphp »

You can view the list of pdf files. Imagine that there are 100's of files and you need to find a specific file.
How do I do this?
Thanks for your help.
Last edited by dwhphp on Thu Feb 02, 2012 7:53 pm, edited 2 times in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: find a pdf file

Post by Celauran »

readdir() + preg_match() should do the trick.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: find a pdf file

Post by califdon »

First of all, I suggest that you remove the username/password info in your last post, a hacker reading this forum could easily do a lot of damage to your site!

You need to define "find a specific file" more clearly. Is the user going to know the filename? Do you want to do a keyword search? There must be several different ways to "find a specific file", depending on what you want the user to do. Perhaps you would allow the user to specify a partial filename, then, using readdir(), you could read all the filenames in the directory, but only add a filename to an array if it matched the user's input, using preg_match(). Then the resulting array could be sent to the browser as a Javascript array and displayed to the user similar to the way you displayed all pdf files, only it will presumably be a much smaller list, allowing the user to select which one to view. You would include logic in case no matches were found, to notify the user that no matches were found. This is more of a user interface design than a PHP issue.
Post Reply