0

How can I choose a upload directory using a select drop down and upload a file to that directory that I chose using PHP?

Basically what I am trying to do is create an upload form so that I may choose what predetermined directory in a drop down to upload the file to. For example if I want to upload 'image.jpg' to the folder 'family' I can then use a drop down, select family and upload the image. So far I have yet to find a script similar to what I am trying to do. I see regular upload scripts, but nothing so that I can choose a predetermined directory. Any ideas?

20th May 2022, 5:28 PM
Nica STRAIT
Nica STRAIT - avatar
2 odpowiedzi
0
<form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">Choose a file:</label> <input type="file" name="file" id="file"><br><br> <label for="directory">Select a directory:</label> <select name="directory" id="directory"> <option value="family">Family</option> <option value="friends">Friends</option> <!-- Add more options for directories as needed --> </select><br><br> <input type="submit" value="Upload File" name="submit"> </form>
30th Sep 2023, 7:22 PM
D1M3
D1M3 - avatar
0
<?php if (isset($_POST['submit'])) { $directory = $_POST['directory']; // Check if the directory exists, if not, create it if (!is_dir($directory)) { mkdir($directory, 0777, true); } $targetDir = $directory . '/'; $targetFile = $targetDir . basename($_FILES['file']['name']); // Check if the file has been successfully uploaded if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) { echo "File uploaded successfully to '$directory'."; } else { echo "Error uploading file."; } } ?>
30th Sep 2023, 7:22 PM
D1M3
D1M3 - avatar