views:

45

answers:

4

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.

+1  A: 
  • 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.
fabrik
When I put the file_exists() within the foreach all the text just disappears.
Andy Cheeseman
$currentfile should always be set via the one variable I will change in each block. That is the $compartment. I.e. $compartment = "1", $compartment = "2" etc...I know that there will only ever be one file named "1" with a random file extention (dependant on what file type the user uploads). Therefore $compartment.* searches for whatever the compartment number is, in any filetype. (The results of which will always either be one or none).I hope this makes sense? :S
Andy Cheeseman
A: 

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

ivan73
nothing displays when I place the if/else inside the foreach
Andy Cheeseman
A: 

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

Tarek Fadel
A: 

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;

?>
Toby Allen