I have created one input type for uploading and deleting a file and a dropdown which consists of 2 options. On selecting dropdown of upload file it shall upload the file and selecting the dropdown of delete file it shall delete the file. After clicking on the submit button the selected option shall be performed. I am facing an error of undefined array.
Front end:
<?php
include "dbconfig.php"; // includes database connection details
?>
<html>
<head>
<title>file upload operations</title>
<style>
* {
box-size: border-box; //
}
form {
display: block; // specifies if/how an element is displayed.
text-align: center; // align the elements in center
margin-top: 30px; //
margin-bottom: 30px;
margin-left: 30px;
margin-right: 30px;
padding-top: 30px;
padding-left: 30px;
padding-right: 30px;
padding-bottom: 30px;
color: black;
font-weight: bold;
}
body {
background-color: #CCCCFF;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto;
height: 100vh;
text-color: white;
}
input[type=submit] {
background-color: #FF6600; // consist of submit button background
color: white; // button text color white
padding: 12px 20px; //
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049; // on hovering submit button it will change to this color
}
</style>
</head>
<body>
<div class="container">
<form action="loadPage.php" method="post" enctype="multipart/form-data" name="theForm" id="theForm"> // it will load loadpage.php which contains switch loop for performing action on selected dropdown Select a file to perform a specific operations
<input type="file" name="fileToUpload" id="fileToUpload"><br><br><br><br> // input type for uploading and deleting the file
<select form="theForm" name="selectedPage">// selects the page
<option value="page_1">upload file </option> // dropdown option for uploading the file
<option value="page_2">delete file </option> // dropdown option for deleting the file
</select><br><br><br><br><br><br> // for next line space after dropdown
<input type="submit" value="Load page" /> this button will load the page based on selected dropdown
<input type="submit" value="Cancel"> // cancel button
</form>
</div>
</body>
</html>
Code for the loading page on basis of selected dropdown:
<?php
$requested_page = $_POST['selectedPage']; // selects the requested page
switch($requested_page) { // switch case for selecting requested page
case "page_1":
header("Location: upload.php");// on selecting upload file upload action will be performed
break;
case "page_2":
header("Location: delete.php");// on selecting upload file upload action will be performed
break;
default :
echo "No page was selected";
break;
}
?>
deletepage.php
<?php
$target_dir = "uploads/"; // directory where files are uploaded
$fileName = basename($_FILES["fileToDelete"]["name"]);
$target_file = $target_dir . basename($_FILES["fileToDelete"]["name"]); // location of target file
if(isset($_POST["Delete"])) //action on clicking deleted button
{
if (!unlink($target_file)) // unlinks the targeted file
{
echo ("$target_file cannot be deleted due to an error"); // error message after file was not deleted due to an error
}
else
{
echo ("$target_file has been deleted"); // error message after deleting selected target file
}
}
?>
I am getting this error after running my code
Warning: Undefined array key "fileToDelete" in
C:\xampp\htdocs\fileuploadoperations\delete.php on line 5
Warning: Trying to access array offset on value of type null in
C:\xampp\htdocs\fileuploadoperations\delete.php on line 5
Warning: Undefined array key "fileToDelete" in
C:\xampp\htdocs\fileuploadoperations\delete.php on line 6
Warning: Trying to access array offset on value of type null in
C:\xampp\htdocs\fileuploadoperations\delete.php on line 6
My Answer:
html
File Upload/Delete Form
_Backend (process.php):_
php
if(isset($_POST['submit'])){
$action = $_POST['action'];
if($action == 'upload'){
$file = $_FILES['file'];
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
move_uploaded_file($fileTmpName, 'uploads/'.$fileName);
echo "File uploaded successfully.";
} elseif($action == 'delete'){
$fileName = $_FILES['file']['name'];
if(file_exists('uploads/'.$fileName)){
unlink('uploads/'.$fileName);
echo "File deleted successfully.";
} else {
echo "File not found.";
}
}
}
?>
Make sure you have a folder named "uploads" in the same directory as your PHP file to store the uploaded files.
Rate this post
3 of 5 based on 6956 votesComments