Php 如何设置传入函数的变量的值?
我正在开发一个允许用户一次“上传”多个文件的页面(它们存储在与其类型相关的本地文件夹中) 我的问题是,当我尝试将Php 如何设置传入函数的变量的值?,php,html,function,scope,Php,Html,Function,Scope,我正在开发一个允许用户一次“上传”多个文件的页面(它们存储在与其类型相关的本地文件夹中) 我的问题是,当我尝试将$upFile1和$fileInfo1传递到writeResults()以更新$fileInfo1中有关$upFile1的信息时,回显结果为空 我做了一些研究,这似乎是一个范围问题,但由于上个月刚开始学习PHP,我不确定解决这个问题的最佳方法 任何帮助都将不胜感激 foo.html 文件上传 文件1: 文件2: foo.php 使用和符号 使用和符号 使用和符号 使
$upFile1
和$fileInfo1
传递到writeResults()
以更新$fileInfo1
中有关$upFile1
的信息时,回显结果为空
我做了一些研究,这似乎是一个范围问题,但由于上个月刚开始学习PHP,我不确定解决这个问题的最佳方法
任何帮助都将不胜感激
foo.html
文件上传
文件1:
文件2:
foo.php
使用和符号
使用和符号
使用和符号
使用和符号
您正在传递$fileInfo1
和$fileInfo2
的值,但它们是空的。在此之后,$destination
值与fileininfo值之间没有关系
更改函数以返回$destination
值
将writeResults
命令更改为$fileInfo1=writeResults($upFile1)
您正在传递$fileInfo1
和$fileInfo2
的值,但它们是空的。在此之后,$destination
值与fileininfo值之间没有关系
更改函数以返回$destination
值
将writeResults
命令更改为$fileInfo1=writeResults($upFile1)
您正在传递$fileInfo1
和$fileInfo2
的值,但它们是空的。在此之后,$destination
值与fileininfo值之间没有关系
更改函数以返回$destination
值
将writeResults
命令更改为$fileInfo1=writeResults($upFile1)
您正在传递$fileInfo1
和$fileInfo2
的值,但它们是空的。在此之后,$destination
值与fileininfo值之间没有关系
更改函数以返回$destination
值
将writeResults
命令更改为$fileInfo1=writeResults($upFile1)代码>
在$destination
前面添加&
将通过引用而不是通过值传递变量。因此,在函数中所做的修改将应用于传递的变量,而不是函数内部的副本
在$destination
前面添加&
将通过引用而不是通过值传递变量。因此,在函数中所做的修改将应用于传递的变量,而不是函数内部的副本
在$destination
前面添加&
将通过引用而不是通过值传递变量。因此,在函数中所做的修改将应用于传递的变量,而不是函数内部的副本
在$destination
前面添加&
将通过引用而不是通过值传递变量。因此,在函数中所做的修改将应用于传递的变量,而不是函数内部的副本。那么,你到底在谈论哪个函数,你期望它做什么,它做什么,以及为什么你期望范围成为一个问题?我编辑了我的OP以澄清我的问题。那么,你到底在谈论哪个函数,你期望它做什么,它做什么,为什么你期望范围成为一个问题?我编辑了我的OP来澄清我的问题。那么,你到底在说什么功能,你期望它做什么,它做什么,为什么你期望范围成为一个问题?我编辑了我的OP来澄清我的问题。所以,你到底在谈论哪个函数,你期望它做什么,它做什么,为什么你期望范围成为一个问题?我编辑了我的OP来澄清我的问题。你会推荐返回方法而不是通过引用传递变量吗?是的,你可以更好地使用返回值它更干净,在我看来,这两种方法都有效,但返回值会使代码更清晰。至少对我来说,非常感谢。我发现必须坚持最佳实践,这将有助于我将来的函数声明。您建议使用返回方法而不是通过引用传递变量吗?是的,您可以更好地使用返回值。在我看来,这两种方法都很有效,但返回值会使代码更清晰。至少对我来说,非常感谢。我发现必须坚持最佳实践,这将有助于我将来的函数声明。您建议使用返回方法而不是通过引用传递变量吗?是的,您可以更好地使用返回值。在我看来,这两种方法都很有效,但返回值会使代码更清晰。至少对我来说,非常感谢。我发现必须坚持最佳实践,这将有助于我将来的函数声明。您建议使用返回方法而不是通过引用传递变量吗?是的,您可以更好地使用返回值。在我看来,这两种方法都很有效,但返回值会使代码更清晰。至少对我来说,非常感谢。我发现必须坚持最佳实践,这将有助于我将来的函数声明。
<!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>
<?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>
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.
}