dousui3124 2015-02-06 08:16
浏览 32
已采纳

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

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>
  • 写回答

3条回答 默认 最新

  • dqpciw9742 2015-02-06 08:29
    关注

    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);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号