0

我是 PHP 新手,正在尝试创建一个页面,该页面将根据选择的表单按钮进行更改/更新/刷新。这部分有效。该站点的目的是允许上传两种类型的数据的多个文件:照片和音乐,以及文本区域文本的上传——每种类型都存储在不同的位置。我遇到的问题是为每种数据类型指定专用路径(用于 move_uploaded_file 命令)。我可以在变量中设置路径,但似乎if(isset($_FILES['whatever']))代码块看不到这些变量。我没有收到任何错误(如您所见,我正在通过 localhost 进行测试)。我应该如何重新设计我的代码以实现目标?我假设问题出在我处理项目的方式上。

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>```
4

1 回答 1

0

当您提交第二个表单(带有文件的表单)时,您不会重新发送“按钮”值。为了避免这种情况,您可以在“发布条目”-submit-input 上方的文件表单中添加隐藏输入,如下所示:

<input type="hidden" name="Button" value="Music" />
于 2021-02-12T09:43:05.967 回答