0

I have a code to list all my directory without extension and it works well. The thing is when I want to add some php<> before and after, that transform my <?php ?> in <!-- -->. I don't know why. Any thoughts?

   <?php
    $variables = '';
    $handle = '';
    $variablesfile = '';
    // open my directory
    if ($handle = opendir($_SERVER['DOCUMENT_ROOT'].'/mvi/index/graphic-documents/sozie/variables/')) {
        // list directory contents
        while (false !== ($variablesfile = readdir($handle))) {
            // exeptions
            if (($variablesfile != ".")
            && ($variablesfile != "..")
            && ($variablesfile != "index.php")
            && ($variablesfile != "compiler.php")
            && ($variablesfile != ".DS_Store"))

            // only grab file names
            if (is_file($_SERVER['DOCUMENT_ROOT'].'/my/path/'. $variablesfile)) {
                $file_name = $variablesfile;
                $file_array = explode('.',$file_name);
                $extension = count($file_array) - 2;
                $no_extension = substr($file_name,0,strlen($file_array[$extension]));

                // creation of php code 
                $variables .= '<?php $'.$no_extension.' = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/'.$no_extension.'.txt")  ; ?>'. "\n" ; 
            }
        }

        closedir($handle);

        echo $variables ;

    }
    ?>

Result on inspector:

What should be

<?php $variable = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/variable.txt")  ; ?>

is

<!-- ?php $variable = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/variable.txt")  ; ? -->

If I check my source code from Chrome, the <?php ?> displays well.....

Everything works fine everywhere else.. I have checked my apache and I'm running in localhost..So everything is ok on that side.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • When you say "inspector" you mean in your client? ` – tadman Dec 18 '17 at 23:38
  • yes... in Chrome inspector –  Dec 18 '17 at 23:39
  • Possible duplicate of [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – Goma Dec 18 '17 at 23:40
  • and on source code (still in Chrome) the –  Dec 18 '17 at 23:41
  • 2
    You can't send PHP code to your browser. You *must* execute it in the PHP environment. I'm not sure why you've got PHP code emitting PHP code. Maybe you can explain why. – tadman Dec 18 '17 at 23:42
  • @erwin I've already checked those three points ;) but thanks for sharing –  Dec 18 '17 at 23:44
  • @tadman because I want to list all a directory to transform every filename into variable. At the end I want something like this $FileName=file_get_contents(FileName.txt) –  Dec 18 '17 at 23:47
  • Why not put all that into an associative array and at the end do something with it? There's no reason for independent variables here. If anything that's a bad habit to get into. Think in terms of data structures, not piles of arbitrarily named variables. – tadman Dec 18 '17 at 23:49
  • @tadman You right, I will probably switch to a solution like this if I stay stuck... –  Dec 18 '17 at 23:52
  • 1
    Key here is you can't just emit ` – tadman Dec 18 '17 at 23:53
  • @tadman "Key here is you can't just emit –  Dec 18 '17 at 23:56
  • @tadman thanks for sharing your thought –  Dec 18 '17 at 23:58

1 Answers1

1

Remember you have one shot and one shot only to render out whatever you need to do as part of processing this request. As such, you must immediately execute any code that's required to render the response.

Try capturing the results by defining an associative array:

$results = array();

Then insert the data into that structure using your $no_extension variable as the key:

$results[$no_extension] = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/$no_extension.txt");

So at the end you can do whatever you want with that data.

Don't forget that PHP can and will interpolate variables like $no_extension inside of double-quoted strings. This can simplify your code if you're trying to compose a string based on other strings, avoiding repeated concatenation.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 2
    Yes that's wise... I was too much dreaming =)))))) and not efficiently –  Dec 19 '17 at 00:01
  • @Odjone It's pretty easy to get your contexts mixed up when you're working on the web. Sometimes you're writing SQL inside HTML with CSS inside PHP, so there's basically four different languages with their own syntax in play. Keeping your bearings can be a challenge. – tadman Dec 19 '17 at 00:02