1

Could someone help me with this?

I have a folder with some files (without extention)

/module/mail/templates

With these files:

  • test
  • test2

I want to first loop and read the file names (test and test2) and print them to my html form as dropdown items. This works (the rest of the form html tags are above and under the code below, and omitted here).

But I also want to read each files content and assign the content to a var $content and place it in an array I can use later.

This is how I try to achieve this, without luck:

    foreach (glob("module/mail/templates/*") as $templateName)
        {
            $i++;
            $content = file_get_contents($templateName, r); // This is not working
            echo "<p>" . $content . "</p>"; // this is not working
            $tpl = str_replace('module/mail/templates/', '', $templatName);
            $tplarray = array($tpl => $content); // not working
            echo "<option id=\"".$i."\">". $tpl . "</option>";
            print_r($tplarray);//not working
        }
4
  • what's going wrong? Explain what you mean by "not working" Commented Aug 5, 2012 at 1:00
  • var_dump( $templateName ); right at the top of your loop. My guess is glob() isn't picking up any files with that pattern. Commented Aug 5, 2012 at 1:03
  • The loop runs but does not echo the $content var and print_r does not get printed. So I assume its not picking up or I am doing something wrong. Maybe there is a better way to do this. But I can't see where its going wrong, since it just runs without errors, but does not do what I am trying to. Commented Aug 5, 2012 at 1:04
  • var_dump( $templateName ); did not return anything. But my $tpl var is getting the filenames fine Commented Aug 5, 2012 at 1:06

2 Answers 2

1

This code worked for me:

<?php
$tplarray = array();
$i = 0;
echo '<select>';
foreach(glob('module/mail/templates/*') as $templateName) {
    $content = file_get_contents($templateName); 
    if ($content !== false) {
        $tpl = str_replace('module/mail/templates/', '', $templateName);
        $tplarray[$tpl] = $content; 
        echo "<option id=\"$i\">$tpl</option>" . PHP_EOL;
    } else {
        trigger_error("Cannot read $templateName");
    } 
    $i++;
}
echo '</select>';
print_r($tplarray);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Initialize the array outside of the loop. Then assign it values inside the loop. Don't try to print the array until you are outside of the loop.

The r in the call to file_get_contents is wrong. Take it out. The second argument to file_get_contents is optional and should be a boolean if it is used.

Check that file_get_contents() doesn't return FALSE which is what it returns if there is an error trying to read the file.

You have a typo where you are referring to $templatName rather than $templateName.

$tplarray = array();
foreach (glob("module/mail/templates/*") as $templateName) {
        $i++;
        $content = file_get_contents($templateName); 
        if ($content !== FALSE) {
            echo "<p>" . $content . "</p>";
        } else {
            trigger_error("file_get_contents() failed for file $templateName");
        } 
        $tpl = str_replace('module/mail/templates/', '', $templateName);
        $tplarray[$tpl] = $content; 
        echo "<option id=\"".$i."\">". $tpl . "</option>";
}
print_r($tplarray);

6 Comments

The r in file_get_contents would be from fopen.
Yea sorry, I started using fopen, and forgot to change it. Thanks a lot for your help. Now I get the filenames again, but still no output from print_r and echo $content
Also, it would be more readable to have echo '<option id="'.$i.'">". $tpl . '</option>'; or echo "<option id=\"$i\">$tpl</option>";.
@Wiseguy Yes, I realized my error and corrected the code above to reflect the intended result. It now says $tplarray[$tpl] = $content; Thanks!
Set your error logging high and check your error log. trigger_error() is probably firing, indicating that the file_get_contents() is failing. If you have no idea where your logs are and just want to get this done, change trigger_error() to echo(). Later, go figure out where your logs are and how to set your PHP logging settings because that will help you a lot when trying to debug stuff.
|

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.