简体   繁体   English

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

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

I am working on a page that allows the user to "upload" multiple files at once (they are stored locally in folders relative to their type). 我在一个页面上工作,该页面允许用户一次“上传”多个文件(它们相对于它们的类型本地存储在文件夹中)。

My problem is that when I try to pass $upFile1 and $fileInfo1 to writeResults() to update $fileInfo1 with information about $upFile1 , the echoed result is empty. 我的问题是,当我尝试将$upFile1$fileInfo1传递给writeResults()以使用有关$upFile1信息更新$fileInfo1时,回显的结果为空。

I did some research and this appears to be a scoping issue, but I'm not sure about the best way to get around this having just started learning PHP last month. 我做了一些研究,这似乎是一个范围界定的问题,但是我不确定上个月刚开始学习PHP时解决该问题的最佳方法。

Any help would be greatly appreciated. 任何帮助将不胜感激。

foo.html 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 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>

you are passing the values of $fileInfo1 and $fileInfo2 but they are empty. 您正在传递$fileInfo1$fileInfo2的值,但它们为空。 After that there is no relation between the $destination value and the fileininfo values. 之后, $destination值和fileininfo值之间没有关系。

Change your function to return the $destination value. 更改您的函数以返回$destination值。

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

Use the & sign to pass variables by reference 使用&符号通过引用传递变量

function addOne(&$x) {
    $x = $x+1;
}
$a = 1;
addOne($a);
echo $a;//2
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.
}

Adding & in front of $destination will pass the variable by reference, instead of by value. $destination前面添加&将通过引用而不是通过值传递变量。 So modifications made in the function will apply to the variable passed, instead of a copy inside the function. 因此,在函数中进行的修改将应用于传递的变量,而不是函数内部的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cakephp - 如何设置从控制器传递到视图的变量的默认值? - cakephp - How can I set a default value of a variable passed from controller to view? 我可以在php中为传递的函数参数添加变量值吗? - Can I add a variable value to a passed function parameter in php? 在PHP中,如何获取函数调用中传入的变量名? - In PHP, how can I get the variable name that passed in in a function call? 如何使用传递给JavaScript函数的输入参数值到此JS函数打开的弹出窗口中? - How can I use the input parameter value passed to a JavaScript function into the popup opened by this JS function? 如何将JS变量设置为SOAP函数返回的值? - How can I set a JS variable to what a SOAP Function returns? 我不能将变量设置为文本框值 - Can't I set a variable as a textbox value 在PHP中如何调用具有传递给它的相同变量的函数? - In PHP how can I call a function with the same variables that were passed to it? 如何将动态数量的属性传递给函数? - How can I pass a dynamic amount of attributes passed to a function? 我如何打印变量,因为它不是变量的值 - how i can print the variable as it not the value of the variable 如何使用PHPUnit模拟通过引用传递变量的函数? - How to mock a function that variable be passed by reference with PHPUnit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM