如何设置传递给函数的变量的值?

How can I set the value of a variable passed into a function?

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

我的问题是,当我尝试将 $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>

使用 & 符号 pass variables by reference

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

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

将函数更改为 return $destination 值。

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

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前面添加&将通过引用传递变量,而不是通过值传递。因此在函数中所做的修改将应用于传递的变量,而不是函数内部的副本。