开发者

Variables are not being rewritten as page is processed

开发者 https://www.devze.com 2023-01-05 14:18 出处:网络
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,

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 $f开发者_如何转开发ilename) {
    $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.


  • 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.


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


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


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;

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号