Why does not the loop in PHP?

Hello! From web programming I am very far, but it so happened that I need to do the same task, but as it turned out I can't handle it.
And so, to begin I need when the user choose different options using radio button to send the file to the desired folder.
Here is the code which I have:
<?php
include_once('functions.php')
?>
<!DOCTYPE html>
<html>
the <head>
 <meta charset="utf-8">
 <title>Hochladen von Bildern auf den Server</title>
</head>

the <body>

 <form action="functions.php" method="post">
 A4 <input type="radio" name="ans" value="ans1">
 A3 <input type="radio" name="ans" value="ans2" /><br />

</form>

 <form method="post" enctype="multipart/form-data">
 <input type="file" name="file">
 <input type="submit" value="Datei herunterladen!">
</form>


<?php
 // if were sending the form
 if(isset($_FILES['file'])) {
 // check whether we can load the image
 $check = can_upload($_FILES['file']);

 if($check === true){
 // load an image to the server
make_upload($_FILES['file']);
 echo "<strong>Die Datei wurde erfolgreich heruntergeladen!</strong>";
}
else{
 // print error message
 echo "<strong>$check</strong>"; 
}
}
?>
</body>
</html>

And
<?php
 function can_upload($file){
 // if name is empty, the file is not selected
 if($file['name'] == ")
 return 'You did not select a file.';

 /* if the file size is 0, it means that he was not allowed settings 
 servers due to the fact that it is too big */
 if($file['size'] == 0)
 return 'File too large.';

 // split the file name by dot and get an array of
 $getMime = explode('.', $file['name']);
 // we are interested in the last element of the array - extension
 $mime = strtolower(end($getMime));
 // declare an array of acceptable extensions
 $types = array('jpg', 'png', 'gif', 'bmp', 'jpeg', 'pdf', 'ppt', 'ods', 'dotx', 'docx', 'doc');

 // if the extension is not included in the list of valid - return
 if(!in_array($mime, $types))
 return 'invalid file type.';

 return true;
}



if ($answer == "ans1") {
$answer = $_POST['ans']; 
 make_upload function($file){ 
 // generated unique image name: random number and name
 $name = mt_rand(0, 10000) . $file['name'];
 copy($file['tmp_name'], 'Folder_A4_SW_EIN/' . $name); //FOLDER DOC
 return true; 
}
}else{
 make_upload function($file){ 
 // generated unique image name: random number and name
 $name = mt_rand(0, 10000) . $file['name'];
 copy($file['tmp_name'], 'img7/' . $name); //the FOLDER WHERE PADAYUT DOCUMENT
 } 
} 
?>

And here the problem is that what I would do, documents are always falling into the same folder img7. No matter which radio button I chose.
Tell someone what is wrong and how can I fix it?
Thank you!
March 23rd 20 at 19:28
2 answers
March 23rd 20 at 19:30
Solution
Error with forms, radio buttons you have on one form, and send the file to you really using another ATP. tov. @Joanie
code
<form action="functions.php" method="post">
 A4 <input type="radio" name="ans" value="ans1">
 A3 <input type="radio" name="ans" value="ans2" /><br />
</form>

 <form method="post" enctype="multipart/form-data">
 <input type="file" name="file">
 <input type="submit" value="Datei herunterladen!">
 </form>



It is necessary to make approximately so
code
<form action="#" method="post" enctype="multipart/form-data">
 A4 <input required type="radio" name="ans" value="ans1">
 A3 <input type="radio" name="ans" value="ans2">
 <input type="file" name="file">
 <input type="submit" value="Upload">
 </form>


Check the condition, and then declare a variable, of course you have the condition is not met.
code
if ($answer == "ans1") {
$answer = $_POST['ans'];

We must first declare a variable, then do with it whatever you want. But in this case, in the function Declaration it is not the place. Cm. trail. item.
code
$answer = $_POST['ans']; 
if ($answer == "ans1") {

As noted by @Joanie so functions cannot be declared
code
if ($answer == "ans1") {
$answer = $_POST['ans']; 
 make_upload function($file){ 
 // generated unique image name: random number and name
 $name = mt_rand(0, 10000) . $file['name'];
 copy($file['tmp_name'], 'Folder_A4_SW_EIN/' . $name); //FOLDER DOC
 return true; 
}
}else{
 make_upload function($file){ 
 // generated unique image name: random number and name
 $name = mt_rand(0, 10000) . $file['name'];
 copy($file['tmp_name'], 'img7/' . $name); //the FOLDER WHERE PADAYUT DOCUMENT
 } 
}


Take the name as a separate argument
code
function make_upload($file, $folder)
{ 
 // generated path + unique name pictures
 $name = $folder . DIRECTORY_SEPARATOR . uniqid(true) . $file['name'];
 copy($file['tmp_name'], $name);
 return true; 
}
UPD: working g-code but
<?php
include_once('functions.php')
?>
<!DOCTYPE html>
<html>
the <head>
 <meta charset="utf-8">
 <title>upload image to server</title>
</head>
the <body>
 <form action="#" method="post" enctype="multipart/form-data">
 A4 <input required type="radio" name="ans" value="ans1">
 A3 <input type="radio" name="ans" value="ans2">
 <input type="file" name="file">
 <input type="submit" value="Upload">
</form>
<?php
 // if were sending the form
if(isset($_FILES['file']))
{
 // check whether we can load the image
 $check = can_upload($_FILES['file']);

 if($check === true)
{
 $folder = $_POST['ans'] == 'ans1' ? 'Folder_A4_SW_EIN' : 'img7';
 make_upload($_FILES['file'], $folder); // load an image to the server
 echo "<strong>File was successfully uploaded!</strong>";
}
else
{
 // print error message
 echo "<strong>$check</strong>"; 
}
}
?>
</body>
</html>

<?php
 function can_upload($file)
{
 // if name is empty, the file is not selected
 if($file['name'] == ")
 return 'You did not select a file.';

 /* if the file size is 0, it means that he was not allowed settings 
 servers due to the fact that it is too big */
 if($file['size'] == 0)
 return 'File too large.';

 // split the file name by dot and get an array of
 $getMime = explode('.', $file['name']);

 // we are interested in the last element of the array - extension
 $mime = strtolower(end($getMime));

 // declare an array of acceptable extensions
 $types = array('jpg', 'png', 'gif', 'bmp', 'jpeg', 'pdf', 'ppt', 'ods', 'dotx', 'docx', 'doc');

 // if the extension is not included in the list of valid - return
 if(!in_array($mime, $types))
 return 'invalid file type.';

 return true;
}

 function make_upload($file, $folder)
 { 
 // generated path + unique name pictures
 $name = $folder . DIRECTORY_SEPARATOR . uniqid(true) . $file['name'];
 copy($file['tmp_name'], $name);
 return true; 
 }

Thanks, this error is corrected, but the result is the same) - Mikayla.Johns commented on March 23rd 20 at 19:33
Duplication not notice? - Joanie commented on March 23rd 20 at 19:36
@Joanie, I noticed, thanks. - chloe_Hammes83 commented on March 23rd 20 at 19:39
March 23rd 20 at 19:32
Each form works individually. Combine them into one and you will have both affordable and $_POST['ans'] and $_FILES['file'].

Find more questions by tags PHP