我可以将变量传递给 if(isset($_FILES['whatever'])) 逻辑过程吗?
Can I pass a variable into an if(isset($_FILES['whatever'])) logical process?
我是 PHP 的新手,我正在尝试创建一个页面,该页面将 change/update/refresh 取决于选择的表单按钮。这部分有效。该网站的目的是允许上传两种类型的数据的多个文件:照片和音乐,以及上传文本区域文本 - 每种类型都存储在不同的位置。我 运行 遇到的问题是为每种数据类型指定路径(用于 move_uploaded_file 命令)。我可以在变量中设置路径,但 if(isset($_FILES['whatever']))
代码块似乎看不到这些变量。我没有收到任何错误(如您所见,我正在通过本地主机进行测试)。我应该如何重新设计我的代码来完成 objective?我假设问题出在我处理项目的方式上。
ini_set('display_errors',1);
$MasterFolder= __DIR__;
$BodyText = "";
$BodyHeader='<form action="" method="post">
<div id="NavContainer" style="margin: 4% auto;">
<input type="submit" name="Button" value="Photos">
<input type="submit" name="Button" value="Writings">
<input type="submit" name="Button" value="Music">
</div>
</form>';
switch (htmlspecialchars($_POST['Button'])) {
case 'Photos':
$ParentFolder = $MasterFolder . '/Photo';
$DestinationFolder = $ParentFolder . '/PhotoStore';
$BodyText='<!-- Whatever -->';
break;
case 'Writings':
$ParentFolder = $MasterFolder . '/Write';
$DestinationFolder = $ParentFolder . '/WriteStore';
$BodyText='<!-- Whatever -->';
break;
case 'Music':
$ParentFolder = $MasterFolder . '/Music';
$DestinationFolder = $ParentFolder . '/musicStore';
$BodyText='<div class="EntryForm">
<div class="TextLabel">Music</div>
<form action="" style="margin: 0 auto 4% auto;" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" multiple="multiple" />
</div>
<div class="EntryForm">
<div class="TextLabel">Description:</div>
<textarea name="MusicText"></textarea>
</div>
<div class="EntryForm">
<input type="submit" value="Publish Entry" />
</p>
</form>
</div> <!-- End ENTRYFORM div -->';
#echo $ParentFolder; ################## These two lines print the expected ##############
#echo "\n" . $DestinationFolder; #################### values #############################
break;
}
if (isset($_FILES['myfile'])) {
#echo $ParentFolder; ################## These two lines are equal to "" ##############
#echo "\n" . $DestinationFolder; ###### and I expected them to contain the values assigned in the switch/case statement above ########
foreach($_FILES['myfile']['tmp_name'] as $key => $tmp_name) {
$DFile = $DestinationFolder . htmlspecialchars($_FILES['myfile']['name'][$key]);
chmod($ParentFolder, 0777);
chmod($DestinationFolder, 0777);
move_uploaded_file($tmp_name, $DFile);
chmod($DFile, 0644);
}
chmod($DestinationFolder, 0650);
chmod($ParentFolder, 0650);
}
?>
<!DOCTYPE html>
<html lang="en">
<head><title>Publish</title>
</head>
<body>
<form action="" method="post">
<input type="submit" name="Button" value="Photos">
<input type="submit" name="Button" value="Writings">
<input type="submit" name="Button" value="Music">
</form>
<div class="EntryForm">
<?php echo $BodyText; ?>
</body>
</html>```
当您提交第二个表单(带有文件的表单)时,您不会重新发送 'Button' 值。为了避免这种情况,您可以在 'Publish Entry'-submit-input 上方的文件表单中添加一个隐藏的输入,如下所示:
<input type="hidden" name="Button" value="Music" />
我是 PHP 的新手,我正在尝试创建一个页面,该页面将 change/update/refresh 取决于选择的表单按钮。这部分有效。该网站的目的是允许上传两种类型的数据的多个文件:照片和音乐,以及上传文本区域文本 - 每种类型都存储在不同的位置。我 运行 遇到的问题是为每种数据类型指定路径(用于 move_uploaded_file 命令)。我可以在变量中设置路径,但 if(isset($_FILES['whatever']))
代码块似乎看不到这些变量。我没有收到任何错误(如您所见,我正在通过本地主机进行测试)。我应该如何重新设计我的代码来完成 objective?我假设问题出在我处理项目的方式上。
ini_set('display_errors',1);
$MasterFolder= __DIR__;
$BodyText = "";
$BodyHeader='<form action="" method="post">
<div id="NavContainer" style="margin: 4% auto;">
<input type="submit" name="Button" value="Photos">
<input type="submit" name="Button" value="Writings">
<input type="submit" name="Button" value="Music">
</div>
</form>';
switch (htmlspecialchars($_POST['Button'])) {
case 'Photos':
$ParentFolder = $MasterFolder . '/Photo';
$DestinationFolder = $ParentFolder . '/PhotoStore';
$BodyText='<!-- Whatever -->';
break;
case 'Writings':
$ParentFolder = $MasterFolder . '/Write';
$DestinationFolder = $ParentFolder . '/WriteStore';
$BodyText='<!-- Whatever -->';
break;
case 'Music':
$ParentFolder = $MasterFolder . '/Music';
$DestinationFolder = $ParentFolder . '/musicStore';
$BodyText='<div class="EntryForm">
<div class="TextLabel">Music</div>
<form action="" style="margin: 0 auto 4% auto;" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" multiple="multiple" />
</div>
<div class="EntryForm">
<div class="TextLabel">Description:</div>
<textarea name="MusicText"></textarea>
</div>
<div class="EntryForm">
<input type="submit" value="Publish Entry" />
</p>
</form>
</div> <!-- End ENTRYFORM div -->';
#echo $ParentFolder; ################## These two lines print the expected ##############
#echo "\n" . $DestinationFolder; #################### values #############################
break;
}
if (isset($_FILES['myfile'])) {
#echo $ParentFolder; ################## These two lines are equal to "" ##############
#echo "\n" . $DestinationFolder; ###### and I expected them to contain the values assigned in the switch/case statement above ########
foreach($_FILES['myfile']['tmp_name'] as $key => $tmp_name) {
$DFile = $DestinationFolder . htmlspecialchars($_FILES['myfile']['name'][$key]);
chmod($ParentFolder, 0777);
chmod($DestinationFolder, 0777);
move_uploaded_file($tmp_name, $DFile);
chmod($DFile, 0644);
}
chmod($DestinationFolder, 0650);
chmod($ParentFolder, 0650);
}
?>
<!DOCTYPE html>
<html lang="en">
<head><title>Publish</title>
</head>
<body>
<form action="" method="post">
<input type="submit" name="Button" value="Photos">
<input type="submit" name="Button" value="Writings">
<input type="submit" name="Button" value="Music">
</form>
<div class="EntryForm">
<?php echo $BodyText; ?>
</body>
</html>```
当您提交第二个表单(带有文件的表单)时,您不会重新发送 'Button' 值。为了避免这种情况,您可以在 'Publish Entry'-submit-input 上方的文件表单中添加一个隐藏的输入,如下所示:
<input type="hidden" name="Button" value="Music" />