Tutorial :Variables are not being rewritten as page is processed



Question:

I have a php script that checks to see if a particular file exists. This name of the file is defined by the 'compartment' variable. When the script is copied and pasted again into a separate block, changing only the compartment variable it runs into a problem...

Say for example 1.jpeg exists but 2.jpeg doesn't. The first block displays a link to this file, but so does the second block when it should be displaying the upload form as 2.jpeg doesn't exist.

It's as though the $currentfile or $filename variables are being carried over into the blocks below them.

Please find an example of my problem below...

<?php      $compartment = "1";        foreach (glob("$compartment.*") as $filename) {      $currentfile = "$filename";      }        if (file_exists($currentfile)) {              echo "              /* If the file exists, it will display a link to the file. */              <a href='$currentfile' target='_blank'>LAUNCH PREVIEW</a>              ";      } else {          echo "              /* Here is an uploader form that would transform foobar.jpeg into $compartment.jpeg. */              ";          }  ?>      <?php      $compartment = "2";        foreach (glob("$compartment.*") as $filename) {      $currentfile = "$filename";      }        if (file_exists($currentfile)) {              echo "              /* If the file exists, it will display a link to the file. */              <a href='$currentfile' target='_blank'>LAUNCH PREVIEW</a>              ";      } else {          echo "              /* Here is an uploader form that would transform foobar.jpeg into $compartment.jpeg. */              ";          }  ?>  

Thank You.


Solution:1

  • Maybe your file_exists() must be inside of foreach otherwise $currentfile always be the last file found in the directory.
  • $filename isn't containing path variable
  • Your logic seems a little bit weird for me. You iterate through a dir and checks every file inside if file_exists or not. Because no other checking (against a prepopulated array for example) happens this will always return true.


Solution:2

foreach will fail to execute (and should yell at you) if you provide a non-array variable. Therefore since 2.jpeg doesn't exist, glob() will return NULL making foreach not execute. However, you are assigning $currentfile within a foreach that never executes so $currentfile will keep its old value "1.jpeg".

The reason this might appear to work the other way around (when $compartment = 1) is because $currentfile is initialized with garbage on first use which is in if(file_exists($currentfile)). This of course evaluates to false so execution jumps to the else part.

HTH


Solution:3

place whole if/else block inside foreach and replace file_exists($currentfile) with file_exists($filename);


Solution:4

Seperate sections in a .php file are part of the same namespace / block / execution. If you use a variable in your first section, it will still be defined and and still have the same value in your second section.

There is no difference between

<?php     $MyValue = 'Value';    ?>  <?php   echo $MyValue;  ?>  

and

<?php     $MyValue = 'Value';  echo $MyValue;    ?>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »