0

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>```
Fuzz
  • 3
  • 2
  • You can't name all your submit buttons with the same name. Since they have the same name, the first two will be ignored and you'll only send the last one ("Music" in this case). For this, you either use: 1) Three different forms, or 2) Another item that allows you to choose between options (like a radio button or a dropdown). – Alejandro Iván Feb 12 '21 at 03:01
  • Thank you Alejandro, but using the Buttons as an array does work. That part seems to function propertly - you can change the "" portions in the code above to see that the Buttons in an array do, indeed, process correctly. – Fuzz Feb 12 '21 at 03:16
  • What if you try adding a `default:` case to see if it goes into it? – Alejandro Iván Feb 12 '21 at 03:17
  • Thanks, Alejandro. Great idea, but same result. Additional info: If I hard-code the paths into the ```if(isset($_FILES['myfile]))``` statement block, it will work for whichever hard-coded path I enter. It's just that I can't seem to figure out a way to successfully pass the variable from the switch/case to the ```if(isset``` code block. – Fuzz Feb 12 '21 at 03:40
  • Hah! That! Declare the variables before the switch statement (at the top, like with $MasterFolder) with an empty value (ex: `$ParentFolder = “”`) and then they will get reassigned in the switch. The problem is that those variables are in scope of the actual switch and you need an outer scope (“higher level”) for them to persist. When their current scope ends, the variable gets released. – Alejandro Iván Feb 12 '21 at 04:10
  • Thanks again, Alejandro... when I try that, I still have no luck. When I test ```$ParentFolder``` and ```$DestinationFolder``` within the ```if(isset($_FILES``` block, they still show "". – Fuzz Feb 12 '21 at 04:26
  • I know this is pretty obvious, but are you actually submitting the form? – Alejandro Iván Feb 12 '21 at 04:29
  • Alejandro!!!! Progress! If I assign the initializations something other than "" - and make ```$ParentFolder = "Test1"``` and ```$DestinationFolder = "Test2"``` then these values DO show up properly within the ```if(isset($_FILES``` block!!! So, it seems that the problem still resides within the switch/case definitions of $ParentFolder and $DestinationFolder? – Fuzz Feb 12 '21 at 04:35
  • Hmm it might be. Just for curiosity, what does `echo $_POST[‘Button’]` print? More precisely, `echo htmlspecialchars($_POST[‘Button’]);`? – Alejandro Iván Feb 12 '21 at 04:40
  • The results are: "Photos" "Writings" and "Music" if I place the ```echo (htmlspecialchars($_POST['Button']);``` command within the three case blocks. However, if I place the command in the ```if(isset[$_FILES``` block, it does nothing. (Also, to answer your earlier question...yes, the buttons ARE submitted.) – Fuzz Feb 12 '21 at 04:54

1 Answers1

0

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" />
DrMaxNix
  • 73
  • 5