1

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>"; 
}
4
  • so is the problem that you cannot get the file in the first place in order to then parse into an array to check of can you actually GET the file values its the next bit thats tricky? Commented Jan 22, 2014 at 13:46
  • 1
    htaccess does 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. Commented Jan 22, 2014 at 13:48
  • There might be a permissions problem, but in the case of a local file and PHP, .htaccess is irrelevant. What does file_get_contents() return? Try var_dump(file_get_contents()); - does it return false? If so, then the call indeed fails, but for any number of reasons. Commented Jan 22, 2014 at 13:51
  • Thanks guys for your answer, It is resolved by @voodoo417 method: Commented Jan 22, 2014 at 13:59

2 Answers 2

1
$content_array = file("file.tpl");
if (strpos(implode(' ',$content_array), "a2")) 
{
  echo "Match found<br>"; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this!

$content = file_get_contents("file.tpl");
$striping = explode(" ", $content);
foreach($striping as $strip) 
{ 
    if($strip == 'a2')
    {
        echo "Match found<br />";
    }
}

Don't forget it's <br /> same as in <hr /> you open it and you close it in the same time.

2 Comments

It says: Warning: file_get_contents(bla bla..) [function.file-get-contents]: failed to open stream: No such file or directory in /home/user/public_html/site.com/dir/file.tpl on line 3.. My .htaccess is like this # Prevent Direct Access to files <FilesMatch "\.(tpl|ini|log)"> Order deny,allow Deny from all </FilesMatch> Maybe this .htaccess setting that make file_get_contetns() method doesn't work.
Try to change file_get_contents to file. And if you change the file don't use / to go back to the root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.