phphtmlarraysbuttonmove-uploaded-file

Can I pass a variable into an if(isset($_FILES['whatever'])) logical process?


I am new to PHP and am trying to create a page which will change/update/refresh depending on which form buttons are selected. This part works. The purpose of the site is to allow multiple file uploads of two types of data: PHOTO and MUSIC, along with uploads of textarea text - with each type being stored in a different location. The problem I've run into is in dedicating paths for each data type (for use with the move_uploaded_file command). I can set the paths in variables, but it seems the if(isset($_FILES['whatever'])) block of code cannot see those variables. I receive no errors (as you can see, I'm testing via localhost). How should I redesign my code to accomplish the objective? I'm assuming that the problem is in the way I'm approaching the project in general.

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

Solution

  • When you submit the second form (the one with the file) you don't re-send the 'Button' value. To circumvent this you could add a hidden input to your file-form above the 'Publish Entry'-submit-input like this:

    <input type="hidden" name="Button" value="Music" />