【问题标题】:How can I set the value of a variable passed into a function?如何设置传递给函数的变量的值?
【发布时间】:2015-04-06 07:59:10
【问题描述】:

我正在开发一个允许用户一次“上传”多个文件的页面(它们在本地存储在与其类型相关的文件夹中)。

我的问题是,当我尝试将$upFile1$fileInfo1 传递给writeResults() 以使用有关$upFile1 的信息更新$fileInfo1 时,回显结果为空。

我做了一些研究,这似乎是一个范围界定问题,但我不确定解决这个问题的最佳方法是上个月刚开始学习 PHP。

任何帮助将不胜感激。

foo.html

<!DOCTYPE HTML>

<html>
    <head>
        <title>File Upload</title>
    </head>
    <body>
        <form method="post" action="foo.php" enctype="multipart/form-data">
            <p>
                <b>File 1:</b><br>
                <input type="file" name="upFile1"><br/>
                <br/>
                <b>File 2:</b><br>
                <input type="file" name="upFile2"><br/>
                <br/>
            </p>
            <p>
                <input type="submit" name="submit" value="Upload Files">
            </p>
        </form>
    </body>
</html>

foo.php

<?php

$upFile1 = $_FILES['upFile1'];
$upFile2 = $_FILES['upFile2'];

$fileInfo1 = "";
$fileInfo2 = "";

// Check if directories exist before uploading files to them
if (!file_exists('./files/images')) mkdir('./files/images', 0777, true);
if (!file_exists('./files/text')) mkdir('./files/text', 0777, true);

// Copies the file from the source input to its corresponding folder
function copyTo($source) {
    if (($source['type'] == 'image/jpg') || ($source['type'] == 'image/png')) {
        @copy($source['tmp_name'], "./files/images/".$source['name']);
    }
    if ($source['type'] == 'text/plain') {
        @copy($source['tmp_name'], "./files/text/".$source['name']);
    }
}

// Outputs file data for input file to destination
function writeResults($source, $destination) {
    $destination .= "You sent: ";
    $destination .= $source['name'];
    $destination .= ", a ";
    $destination .= $source['size'];
    $destination .= "byte file with a mime type of ";
    $destination .= $source['type'];
    $destination .= ".";
    // echoing $destination outputs the correct information, however
    // $fileInfo1 and $fileInfo2 aren't affected at all.
}

// Check if both of the file uploads are not empty
if ((!empty($upFile1['name'])) || (!empty($upFile2['name']))) {
    // Check if the first file upload is not empty
    if (!empty($upFile1['name'])) {
        copyTo($upFile1);
        writeResults($upFile1, $fileInfo1);
    }
    // Check if the second file upload is not empty
    if (!empty($upFile2['name'])) {
        copyTo($upFile2);
        writeResults($upFile2, $fileInfo2);
    }
} else {
    die("No input files specified.");
}

?>

<!DOCTYPE HTML>

<html>
    <head>
        <title>File Upload</title>
    </head>
    <body>
        <p>
            <!-- This is empty -->
            <?php echo "$fileInfo1"; ?>
        </p>
        <p>
            <!-- This is empty -->
            <?php echo "$fileInfo2"; ?>
        </p>
    </body>
</html>

【问题讨论】:

  • 那么,您到底在谈论哪个功能,您希望它做什么,它会做什么以及为什么您认为范围是一个问题?
  • 我编辑了我的 OP 以澄清我的问题。

标签: php html function scope


【解决方案1】:

使用&amp; 符号到pass variables by reference

function addOne(&$x) {
    $x = $x+1;
}
$a = 1;
addOne($a);
echo $a;//2

【讨论】:

    【解决方案2】:

    您正在传递 $fileInfo1$fileInfo2 的值,但它们是空的。之后,$destination 值和 fileininfo 值之间就没有关系了。

    更改您的函数以返回 $destination 值。

    将您的 writeResults 命令更改为 $fileInfo1 = writeResults($upFile1);

    【讨论】:

    • 你会推荐返回方法而不是通过引用传递变量吗?
    • 是的,你可以更好地使用返回值
    • 我认为它更干净。
    • 两种方法都有效,但返回值会使代码更清晰。至少对我来说。
    • 非常感谢。我发现坚持最佳实践势在必行,这应该对我未来的函数声明有所帮助。
    【解决方案3】:
    function writeResults($source, &$destination) {
        $destination .= "You sent: ";
        $destination .= $source['name'];
        $destination .= ", a ";
        $destination .= $source['size'];
        $destination .= "byte file with a mime type of ";
        $destination .= $source['type'];
        $destination .= ".";
        // echoing $destination outputs the correct information, however
        // $fileInfo1 and $fileInfo2 aren't affected at all.
    }
    

    $destination 前面添加&amp; 将通过引用而不是通过值传递变量。所以在函数中所做的修改将应用于传递的变量,而不是函数内部的副本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-29
      • 2015-10-22
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 2013-04-14
      • 2022-12-08
      • 1970-01-01
      相关资源
      最近更新 更多