0

我试图弄清楚如何在 if 语句之外重用我的 $php_file 和 doc_file 变量?

这是场景...

通常对于每个文件(.pdf/.doc/whatever)上传到文件路径,还有一个 php 文件也被创建。下面是一个快速细分。

myuploaded_file.pdf

myuploaded_file.pdf.php

基本上我试图将这些文件区分为它们自己定义的变量,所以我的最终结果如下......

$doc_file=myuploaded_file.pdf

$php_file=myuploaded_file.pdf.php

然后基本上循环运行该目录中所有上传的文件。

我的位置非常接近我需要的位置,即在每个 if 语句中使用时正确定义了变量,但是我看到了如何在 if 语句之外使用这些变量,因为文件是根据文件类型正确定义到自己的变量中。

如果没有遇到“doc_file 或 php_file 变量未定义”,我什至无法在 if 语句之外回显这些变量。我尝试修改设置全局变量、var_dump 等,但我仍然没有运气。有任何想法吗?

更新代码

<?php
// Open the folder
$f_path = "files/News/"; 
 
$dir_handle = @opendir($f_path) or die("Unable to open $f_path"); 

// Loop through the files 
while ($file = readdir($dir_handle)) { 
    $doc_file="";
    $php_file="";

    // Verify files
    if($file == "." || $file == ".." ) {
        continue;
    }
    // Validate if the file is appended with .php
    if (strpos($file , '.php') !==false) {
        $php_file = $file;
        //echo "PHP: $php_file <br />";
    }
    // Print out the files that don't contain the .php extension
    else {
        $doc_file = $file;          
        //echo "PDF: $doc_file <br />";
    }
echo "PHP: $php_file <br />";
echo "PDF: $doc_file <br />";
}


// Close 
closedir($dir_handle);

?>

循环结果

我正在了解如何避免在结果中返回空白值?

PHP:

PDF:uploaded_file1.pdf

PHP:uploaded_file1.pdf.php

PDF:

PHP:

PDF:uploaded_file1.pdf

PHP:uploaded_file1.pdf.php

PDF:

4

4 回答 4

2

// Open the folder 
$dir_handle = @opendir($f_path) or die("Unable to open $f_path"); 

// Loop through the files 
while ($file = readdir($dir_handle)) { 

//variables to be used
$php_file = "";
$doc_file = "";

// Verify files
if($file == "." || $file == ".." ) 
{
    continue;
}
        // Validate if the file is appended with .php
        if (strpos($file , '.php') !==false)
        {
        $php_file = $file;
        echo "PHP: $php_file <br />";
        }
        // Print out the files that don't contain the .php extension
        else
        {
        $doc_file = $file;          
        echo "PDF: $doc_file <br />";
        }

} 
// Close 
closedir($dir_handle); 
于 2012-09-08T08:22:57.383 回答
1

在您的 if 之外定义它们,它们也将在您的 if 范围之外可用。

// Loop through the files 
while ($file = readdir($dir_handle)) { 
    $doc_file="";
    $php_file="";

    // Verify files
    if($file == "." || $file == ".." ) {
        continue;
    }
    // Validate if the file is appended with .php
    if (strpos($file , '.php') !==false) {
        $php_file = $file;
        echo "PHP: $php_file <br />";
    }
    // Print out the files that don't contain the .php extension
    else {
        $doc_file = $file;          
        echo "PDF: $doc_file <br />";
    }
}

如果你在那个while循环之外也需要它们,就把它们放在那个循环之外。

于 2012-09-08T08:19:05.660 回答
1

默认情况下,您可以在if,中使用变量whileforeach但它们在每个循环中都会被重写

<?php
// Open the folder
$f_path = "files/News/"; 

$dir_handle = @opendir($f_path) or die("Unable to open $f_path"); 

// Loop through the files 
while ($file = readdir($dir_handle)) { 
    $doc_file="";
    $php_file="";

    // Verify files
    if($file == "." || $file == ".." ) {
        continue;
    }
    // Validate if the file is appended with .php
    if (strpos($file , '.php') !==false) {
        $php_file = $file;
        if(trim($php_file) != ""){
            echo "PHP: $php_file <br />";
        }
    }
    // Print out the files that don't contain the .php extension
    else {
        $doc_file = $file;
        if(trim($doc_file) != ""){
            echo "DOC: $doc_file <br />";
        }
    }
}


// Close 
closedir($dir_handle);

?>
于 2012-09-08T08:22:01.223 回答
1
<?php
if (1==1) {
    $abc=3;
}
echo $abc;
?>

这会打印出“3”,这意味着变量已经在 ifs 之外可用。

于 2012-09-08T08:22:57.720 回答