How to read directory -> read content of all files found -> and content to a "if" statements
this is my first question after being an avid reader of this site for a while now.
I have a php script that queries my database, and inside of that I have an if s开发者_运维知识库tatement to allow only certain categories to show up in results.
I am building a script that will allow people to query my database, the script and service will be free, but I would like to be able to open up certain categories for searching by charging a really small fee just to help with server costs. And the best way I can think of doing this is above, by allowing them to purchase a file, placing that file in a certain folder, and have the script automatically know its there and open up a category. Ive never done anything like this before.
Basically the if statement goes like this:
if ($cat == 'Apple' || $cat == 'Orange'){
$returnval = $1 . "|" . $2 . "|" . $3 . "|" . $cat;
}
$1 $2 $3 $cat being what I queried.
My question is:
How can I make this if statement dynamic, by reading unknown files located in a directory? Say if each file inside of it $cat=='Apple'
and after each file read a ||
was placed after it unless the last file found.
What kind of php code would read a directory, then read all contents, making sure it was formatted as $cat=='Apple'
otherwise ignore, placing ||
at the end of contents that match that format, adding the contents to a string, placing it into that if statement above?
I think Im explaining what I would like to learn how to do right, but maybe there is another way.
The reason I want to do this, is because I would like to add functionality to a script I made, based on files located in a certain folder, each file adding functionality just by being in that directory.
Is this possible? Or is there maybe a better way?
You can't really add code to your code with code, but you can use other methods that accomplish the same thing. Mostly, you're probably looking for arrays:
if (in_array($cat, array('Apple', 'Orange', ...))
http://php.net/in_array
As for people purchasing files on your server... There's gotta be a better solution than this, but you'd need to lay out your plans a little better for that to be answerable. Usually anything like that would only involve database queries, not files.
If I understand you right, you want the contents of a directory, then you can compare from there. glob()
accomplishes this: http://us3.php.net/manual/en/function.glob.php
As for user charging, I'd highly suggest you pursue a login type system, I'm not sure of the context in which your database operates, but if it's HTTP accessed, create an html page with <input>
types to collect username/password to validate that the user had the rights to access these categories.
If your context is RESTful, you can have the user information encoded on the url, I know the BestBuy RESTful API does this.
精彩评论