Reputation: 6207
I want to require/include a content from database record, but require/include only accepts files. This thing is related to caching, so I dont want to write anything to file (collisions etc). How to dodge it?
Upvotes: 0
Views: 67
Reputation: 1957
Since others have covered eval()
and the fact that this is a bad idea, I will touch on the topic of writing this "content" to a file. Use tempnam
. This will give you the name of a just-created unique filename with 0600 permissions. You can then open it, write your content, close, then require/include. See Example #1 on the tempnam
man page.
Make sure to check that the return value of tempnam
is not false; make sure to unlink
the file after you are done.
Upvotes: 1
Reputation: 173652
Are you fetching PHP code from a database? If so, you're probably doing it wrong. Ideally you should only store data inside a database, not code.
If you're fetching a PHP structure from the database, consider using a serialize()
'd version of it (or json_encode()
'd).
Maybe I have missed the exact purpose of what you're trying to accomplish, do let me know if I'm on the wrong path with my answer.
Whatever you do, don't rely on eval
unless you really really have to; and even then, don't :)
Upvotes: 3
Reputation: 4439
This is very dangerous, especially if the database content was contributed from a user, but anyway you could use this :
eval($your_database_content);
Upvotes: 1
Reputation: 64429
If it is code, you need to use eval()
, though there are many anti-patterns that involve eval()
The manual says:
Caution The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
Upvotes: 1