处理页面时变量不会被重写

发布于 2024-09-08 16:50:34 字数 1303 浏览 8 评论 0原文

我有一个 php 脚本来检查特定文件是否存在。该文件名由“compartment”变量定义。当脚本被复制并再次粘贴到单独的块中时,仅更改隔间变量就会遇到问题...

例如,1.jpeg 存在,但 2.jpeg 不存在。第一个块显示该文件的链接,但第二个块也显示了上传表单,因为 2.jpeg 不存在。

就好像 $currentfile 或 $filename 变量被转移到它们下面的块中。

请在下面找到我的问题的示例...

<?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. */
            ";
        }
?>

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与他有关 2024-09-15 16:50:34
  • 也许您的 file_exists() 必须位于 foreach 内部,否则 $currentfile 始终是目录中找到的最后一个文件。
  • $filename 不包含路径变量
  • 您的逻辑对我来说似乎有点奇怪。您遍历一个目录并检查其中的每个文件是否存在 file_exists 。因为没有其他检查(例如针对预填充数组)发生,所以这将始终返回 true。
  • 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.
骄兵必败 2024-09-15 16:50:34

如果您提供非数组变量,foreach 将无法执行(并且应该对您大喊大叫)。
因此,由于 2.jpeg 不存在,glob() 将返回 NULL,使得 foreach 不执行。但是,您在永远不会执行的 foreach 中分配 $currentfile,因此 $currentfile 将保留其旧值“1.jpeg”。

这可能看起来以相反的方式工作(当 $compartment = 1 时)的原因是因为 $currentfile 在第一次使用时用垃圾初始化,位于 if (file_exists($currentfile))。这当然评估为 false,因此执行跳转到 else 部分。

华泰

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

贩梦商人 2024-09-15 16:50:34

将整个 if/else 块放入 foreach 中,并将 file_exists($currentfile) 替换为 file_exists($filename);

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

千仐 2024-09-15 16:50:34

.php 文件中的单独部分是同一命名空间/块/执行的一部分。如果您在第一部分中使用变量,它仍然会被定义,并且在第二部分中仍然具有相同的值。

之间没有区别

<?php 

$MyValue = 'Value';

?>
<?php
 echo $MyValue;
?>

<?php 

$MyValue = 'Value';
echo $MyValue;

?>

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;

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文