Joseph Webber
Joseph Webber

Reputation: 2173

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.

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.

Any help would be greatly appreciated.

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>

Upvotes: 0

Views: 71

Answers (3)

Bitwise Creative
Bitwise Creative

Reputation: 4105

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. So modifications made in the function will apply to the variable passed, instead of a copy inside the function.

Upvotes: 0

RST
RST

Reputation: 3925

you are passing the values of $fileInfo1 and $fileInfo2 but they are empty. After that there is no relation between the $destination value and the fileininfo values.

Change your function to return the $destination value.

Change your writeResults command to $fileInfo1 = writeResults($upFile1);

Upvotes: 3

Bart Haalstra
Bart Haalstra

Reputation: 1062

Use the & sign to pass variables by reference

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

Upvotes: 0

Related Questions