0

In this example, I'd like to realise a fileupload center where you could firstly choose a a output subfolder via a dropdown-select! Second, you could choose a file which you want to uplaod via drag&drop function or browse-button and finally, you get further passed on to the target folder with its subfolders where you can watch all the entries!

Hereby, the problem is that the new url after the dropdown select (with attached sub-folder value to the url - eg.In the console, I can see that it correctly turns the original url: './DisplayDirectoryContents/uploads/' into './DisplayDirectoryContents/uploads/Kooperationen/'after choosing the subfolder 'Kooperationen' in the dropdown select) will not be passed on to the upload.php file and therefore the file doesn't get uploaded into the target folder!Instead of the target subfolder "Kooperationen" the file gets just uploaded into the "DisplayDirectoryContents"-folder - 2 levels down from where it's supposed to land!

Hierarchy of folders:

  1. DisplayDirectoryContents (here are the php-files)
  2. uploads with subfolders:
  3. Kontaktlisten, Kooperationen, Vorsorge

I've already tried to work with different approaches but the url string of the "move2folder" variable obviously is not passed on to the second php-file called upload.php!

If I try it with a fixed url value like: $moved2folder = './DisplayDirectoryContentss/uploads/Kooperationen/' it works fine! But, I'd like to solve it for the automated passed on choice of a subfolder out of the dropdown select!

Please, could anyone provide me with some hints to solve this issue? Which kind of code do I have to add to the upload.php in order to get the move2folder-url passed on and the file uploaded to the before chosen subfolder?

  1. fancy_fileupload.php (relevant parts)
<html>
<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">

<div class="fileupload">
<div class="fileupload-head">
<h2><span></span>FileUpload Center</h2></div>

<div id="upload-top">Select Output Folder
  <br></br>
  <select class="fileupload" id="foldername" name="folder" onchange="weiterleitung();" style="color: #fff; background: #007a96; border-radius:0.3em; height:33px; font-size:0.9em; " enctype="multipart/forn-data">
            <option value=""> Select Folder ...</option>
            <option value="Kontaktlisten"> Kontaktlisten </option>
            <option value="Kooperationen"> Kooperationen </option>                       
            <option value="Vorsorge"> Vorsorge </option>                                  
            <option value="cityviews"> cityviews </option>
            <i class="fa fa-folder fa-3x" style="color: #007a96"></i></select>
</div>
         <p> <div id="upload-head"> Select Your File          
         
        <div id="drop">
                Drop Here
<p></p>
                <a>Browse</a>
<input type="file" name="upl" multiple>
          </div></div></p>  
            <ul>
               
             <!-- The file uploads will be shown here -->
      
          </ul>

<p>
  <br><div id="upload-bottom"> Find your fileuploads here <a href="https://f706-86-59-29-15.ngrok.io/home/DisplayDirectoryContents/.uploads.php?uploads/"><p><i class="fa fa-folder fa-3x"   
 style="color: #007a96"></i></p></a></div></br></p>


 </form></div>
<script>
function weiterleitung() {
  var url = "./DisplayDirectoryContents/uploads/"; 
  var selectedValue = document.getElementById("foldername").value;
   console.log(selectedValue);
  /*var folder = document.getElementById("foldername").options[document.getElementById("foldername").selectedIndex].value;*/
// Weiterleitung
var move2folder = url + selectedValue + '/';
console.log(move2folder);
}
weiterleitung();
</script>
</html>
  1. upload.php
<?php


require 'fancy_fileupload.php';
$move2folder = $_POST['move2folder'];

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'jpeg', 'svg', 'gif', 'ico', 'zip', 'xlsx', 'docx', 'pdf', 'mp4', 'avi', 'txt', 'mp3');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
    $file_tmp = $_FILES['upl']['tmp_name'];
    $file_name = $_FILES['upl']['name'];
    /*$move2folder = "./DisplayDirectoryContents/uploads/Kooperationen/";*/
    
    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }
    
    if(move_uploaded_file($file_tmp, $move2folder .$file_name)) {
        /*echo "The file was successfully uploaded!";*/
        echo '{"status":"success"}';
        exit;
    }
    
    }

/*echo '{"status":"error"}';*/ 
echo "Unfortunately. the file was not uploaded properly!";
exit;

?>

ยดยดยด

B C
  • 1
  • 1

0 Answers0