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: upload_file1.pdf

PHP: upload_file1.pdf.php

PDF:

PHP:

PDF: upload_file1.pdf

PHP: upload_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
<?php
if (1==1) {
    $abc=3;
}
echo $abc;
?>

これは「3」を出力します。これは、変数がifsの外部ですでに使用可能であることを意味します。

于 2012-09-08T08:22:57.720 に答える
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 に答える