I want to check if a value/string exists in an array (or an file). I've tried to find a solution from anywhere but I do not get it.
Assume I have a file file.tpl:
a1 a2 a3 b1 b2 b3 c1 c2 c3...
I can resolve it using file_get_contents and strpos method:
$content = file_get_contents("file.tpl");
if (strpos($content, "a2") !== false)
{
echo "Match found<br>";
}
But my problem is this file is denied from direct access by .htaccess, so I can't use file_get_contents method.
# Prevent Direct Access to files
<FilesMatch "\.(tpl|ini|log)">
Order deny,allow
Deny from all
</FilesMatch>
If I use file_get_contents method it displays error like this:
Warning: file_get_contents(bla..bla..bla..) [function.file-get-contents]: failed to open stream: No such file or directory in /home/user/public_html/mysite.com/dir/file.tpl on line 3
I tried using the file() method to split it into an array and I try to find using in_array method, but I do not understand how to apply it.
Maybe like this:
$content_array = file("file.tpl");
if (in_array("a2", $content_array))
{
echo "Match found<br>";
}
htaccessdoes not and will never limit PHP from reading the file (unless it's on a different server) - it disables Apache of ever serving the file. PHP =/= Apache. If you can't read the file it's your file permissions that have a problem - not your webserver/code..htaccessis irrelevant. What doesfile_get_contents()return? Tryvar_dump(file_get_contents());- does it returnfalse? If so, then the call indeed fails, but for any number of reasons.