Not being able to upload images in the directory with the images name in the database?

Code for Insert images in the database:

<?php


$host="localhost";
$username="root";
$pass="";
$db="registration";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Database connection error");
}

// insert query for register page
if(isset($_POST['ronel'])){
     $images = $_FILES['file']['name'];
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["file"]["name"]);

  // Select file type
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Valid file extensions
  $extensions_arr = array("jpg","jpeg","png","gif","pdf");

  // Check extension
  if( in_array($imageFileType,$extensions_arr) )
{

$details=$_POST['details'];
$location=$_POST['location'];
$checkbox=$_POST['checkbox'];
$injured=$_POST['injured'];    
$agegender=$_POST['agegender'];   
$contact=$_POST['contact'];
$empid=$_POST['empid'];
$dept=$_POST['dept'];    
$organization=$_POST['organization'];     
$summary=$_POST['summary']; 
$name=$_POST['name'];    
$outcome=$_POST['outcome'];    
$cause=$_POST['cause'];     
$action=$_POST['action'];
$reportedname=$_POST['reportedname'];
$position=$_POST['position'];    
$organisation=$_POST['organisation'];
$reportedcontact=$_POST['reportedcontact'];
$reporteddept=$_POST['reporteddept'];    
$status="Pending";
$comment=$_POST['comment'];
	
	
	 $query="INSERT INTO `proposals` (`details`,`location`,`date`,`time`,`checkbox`,`injured`,`agegender`,`contact`,`empid`,`dept`  ,`organization`,`summary`,`images`,`outcome`,`cause`,`action`,`reportedname`,`position`,`organisation`,`reportedcontact`,`reporteddept`,`status`,`comment`) VALUES ('$details','$location', current_timestamp(),current_timestamp(),'$checkbox','$injured','$agegender','$contact','$empid','$dept' ,'$organization','$summary','$name','$outcome','$cause','$action','$reportedname','$position','$organisation','$reportedcontact','$reporteddept','$status','$comment')";
	
	$res=mysqli_query($conn,$query);
	if($res){
		$_SESSION['success']="Not Inserted successfully!";
		header('Location:');
	}else{
		echo "<script>alert('Proposal not applied!');</script>";
	}
      // Upload file
     move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);
	}
}
date_default_timezone_set("Asia/Kolkata");
?>

Here is my Input file:

<form class="form-horizontal" method="post" action="" enctype="multipart/form-data">
            <input type="hidden" name="ronel" value="">
<div class="form-group">
                        <label style="position:absolute; left:63%; top:425px;" for="inputEmail" class="col-lg-3"><b>Upload Images Here :</b></label><br><br>
                        <div class="col-lg-9">
                            
                            <input style="position:absolute; left:78%; top:420px;" type="file" name="file" enctype="multipart/form-data" class="form-control" name="incident_reference" onchange="document.getElementById('inc_ref').src = window.URL.createObjectURL(this.files[0]); document.getElementById('inc_ref').className +='_active'; document.getElementById('inc_ref_span').className += '_hidden'">
                        </div><iframe id="inc_ref" class="form-group" width="220px" height="130px" style="position:absolute; left:78%; top:32%;"></iframe>
                    </div>

Here is the error code :

Notice: Undefined index: name in /opt/lampp/htdocs/create-nearmiss.php on line 57

Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory in /opt/lampp/htdocs/create-nearmiss.php on line 80

Warning: move_uploaded_file(): Unable to move '/opt/lampp/temp/phpJJbJRe' to 'uploads/' in /opt/lampp/htdocs/create-nearmiss.php on line 80

This line does not have an extension for the file so it is assumed to be a directory?

can you please provide me with the code on how to? this is line 80?

 move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);

Not sure but you need to add the type so something like:
$name=$_POST['name'].$_POST['type'];

I see you are not doing any real security checks on the upload which is not very safe. Relying on checking the extension is not enough.

I have done this but it still shows an error?

Notice: Undefined index: name in /opt/lampp/htdocs/create-nearmiss.php on line 57

code here

<?php


$host="localhost";
$username="root";
$pass="";
$db="registration";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Database connection error");
}

// insert query for register page
if(isset($_POST['ronel'])){
     $images = $_FILES['file']['name'];
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["file"]["name"]);

    // Select file type
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Valid file extensions
  $extensions_arr = array("jpg","jpeg","png","gif","pdf");

  // Check extension
  if( in_array($imageFileType,$extensions_arr) )
  {

$details=$_POST['details'];
$location=$_POST['location'];
$checkbox=$_POST['checkbox'];
$injured=$_POST['injured'];    
$agegender=$_POST['agegender'];   
$contact=$_POST['contact'];
$empid=$_POST['empid'];
$dept=$_POST['dept'];    
$organization=$_POST['organization'];     
$summary=$_POST['summary']; 
$name=$_POST['name'];
$outcome=$_POST['outcome'];    
$cause=$_POST['cause'];     
$action=$_POST['action'];
$reportedname=$_POST['reportedname'];
$position=$_POST['position'];    
$organisation=$_POST['organisation'];
$reportedcontact=$_POST['reportedcontact'];
$reporteddept=$_POST['reporteddept'];    
$status="Pending";
$comment=$_POST['comment'];
	
	
	 $query="INSERT INTO `proposals` (`details`,`location`,`date`,`time`,`checkbox`,`injured`,`agegender`,`contact`,`empid`,`dept`  ,`organization`,`summary`,`images`,`outcome`,`cause`,`action`,`reportedname`,`position`,`organisation`,`reportedcontact`,`reporteddept`,`status`,`comment`) VALUES ('$details','$location', current_timestamp(),current_timestamp(),'$checkbox','$injured','$agegender','$contact','$empid','$dept' ,'$organization','$summary','$name','$outcome','$cause','$action','$reportedname','$position','$organisation','$reportedcontact','$reporteddept','$status','$comment')";
	
	$res=mysqli_query($conn,$query);
	if($res){
		$_SESSION['success']="Not Inserted successfully!";
		header('Location:');
	}else{
		echo "<script>alert('Proposal not applied!');</script>";
	}
      // Upload file
     move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$images);
	}
}
date_default_timezone_set("Asia/Kolkata");
?>

i cannot seem to find the error now? :frowning:

Not getting you sorry can you pls elaborate more?

That error message says that you are trying to access an array element that does not exist in the array. I presume this is line 57:

$name=$_POST['name'];

Do you have a field called name in your form?

okay after i updated the name field in my form i still get the error message saying : Notice : Undefined index: name in /opt/lampp/htdocs/create-nearmiss.php on line 57

Here is my full code. Kindly help.

<?php

session_start();

if (!isset($_SESSION['username'])) { 
	$_SESSION['msg'] = "You have to log in first"; 
	header('location: login.php'); 
} 


if (isset($_GET['logout'])) { 
	session_destroy(); 
	unset($_SESSION['username']); 
	header("location: login.php"); 
} 
?>

<?php


$host="localhost";
$username="root";
$pass="";
$db="registration";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Database connection error");
}

// insert query for register page
if(isset($_POST['ronel'])){
     $image = $_FILES['name']['name'];
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["name"]["name"]);

    // Select file type
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Valid file extensions
  $extensions_arr = array("jpg","jpeg","png","gif","pdf");

  // Check extension
  if( in_array($imageFileType,$extensions_arr) )
  {

$details=$_POST['details'];
$location=$_POST['location'];
$checkbox=$_POST['checkbox'];
$injured=$_POST['injured'];    
$agegender=$_POST['agegender'];   
$contact=$_POST['contact'];
$empid=$_POST['empid'];
$dept=$_POST['dept'];    
$organization=$_POST['organization'];     
$summary=$_POST['summary']; 
$name=$_POST['name'];
$outcome=$_POST['outcome'];    
$cause=$_POST['cause'];     
$action=$_POST['action'];
$reportedname=$_POST['reportedname'];
$position=$_POST['position'];    
$organisation=$_POST['organisation'];
$reportedcontact=$_POST['reportedcontact'];
$reporteddept=$_POST['reporteddept'];    
$status="Pending";
$comment=$_POST['comment'];
	
	
	 $query="INSERT INTO `proposals` (`details`,`location`,`date`,`time`,`checkbox`,`injured`,`agegender`,`contact`,`empid`,`dept`  ,`organization`,`summary`,`image`,`outcome`,`cause`,`action`,`reportedname`,`position`,`organisation`,`reportedcontact`,`reporteddept`,`status`,`comment`) VALUES ('$details','$location', current_timestamp(),current_timestamp(),'$checkbox','$injured','$agegender','$contact','$empid','$dept' ,'$organization','$summary','$image','$outcome','$cause','$action','$reportedname','$position','$organisation','$reportedcontact','$reporteddept','$status','$comment')";
	
	$res=mysqli_query($conn,$query);
	if($res){
		$_SESSION['success']="Not Inserted successfully!";
		header('Location:');
	}else{
		echo "<script>alert('Proposal not applied!');</script>";
	}
      // Upload file
     move_uploaded_file($_FILES['name']['tmp_name'],$target_dir.$image);
	}
}
date_default_timezone_set("Asia/Kolkata");
?>
<html>

<body style="background-color:F8FFFF;">
    <table style="position:absolute; bottom:83.2%; left:226px; width:66.3%; height:16.5%;">
        <tr>
            <th style="font-weight:600; font-size:18px">ONGC TRIPURA POWER COMPANY LIMITED</th>
        </tr>
        <tr>
            <td style="font-weight:600; font-size:18px">INITIAL EHS INCIDENT REPORT FORM OTPC/SOP/EHS/001/R1</td>
        </tr>
    </table>
    <div class="container">
        <img src="otpc1" alt="Paris" style="width:130px; height:75px; border: 1px solid #ddd;
  border-radius: 4px; position: relative;
  left: 85%; bottom:17px;
  padding: 10px;">

        <img src="otpc" alt="Paris" style="width:170px; height:75px; border: 1px solid #ddd;
  border-radius: 4px; position: relative;
  right:140px; bottom:16px;
  padding: 15px;">
    </div>
    <div class="col-xs-6 col-xs-push-3 well">
        <form class="form-horizontal" method="post" action="" enctype="multipart/form-data">
            <input type="hidden" name="ronel" value="">
            <fieldset style="position:relative; width:95%; top:0px; left:10px;">
                <legend>NEW PROPOSAL NO.</legend>
                <!----left box----------->
                <!----right box----------->
                <div class="col-xs-9">
                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="font-size:16px;color:black;"><b>Details of Incident:</b></label><br>
                        <div class="col-lg-9">

                            <textarea rows="5" cols="110" name="details" class="form-control" maxlength="1000" onkeyup="textCounter(this,'counter4',1000);"></textarea><br><input style="position:absolute; left:75.3%; top:102px;" disabled maxlength="3" size="3" value="1000" id="counter4">
                        </div>
                    </div><br>
                    <fieldset style="position:relative; width:73.05%; height:50px; border:1px solid gray;">
                        <div class="form-group" style="position:relative; left:80px; top:-8px;">
                            <label style="position:relative; right:80px; top:25px;" for="inputEmail" class="col-lg-3"><b>Location:</b></label>
                            <div class="col-lg-9">
                                <textarea name="location" class="form-control" rows="2" cols="56" maxlength="100" onkeyup="textCounter(this,'counter5',100);"></textarea><input style="position:absolute; left:54%; bottom:0px;" disabled maxlength="3" size="2" value="100" id="counter5">
                            </div>
                        </div>

                        <div class="form-group" style="position:absolute; left:638px; bottom:22px;">
                            <label style="position:relative; right:58px; top:20px;" for="inputEmail" class="col-lg-3"><b>Date:</b></label>
                            <div class="col-lg-9">
                                <textarea style="position:relative; top:0px;right:19%;" cols="10" rows="1" disabled><?php echo date('Y-m-d'); ?>
                            </textarea>
                            </div>
                            </div>
                            <div class="form-group" style="position:relative; left:780px; bottom:54px;">
                                <label style="position:relative; right:67px; top:20px;" for="inputEmail" class="col-lg-3"><b>Time:</b></label>
                                <div class="col-lg-9">
                                    <textarea style="position:relative; top:0px;right:3%;" cols="10" rows="1" disabled><?php
date_default_timezone_set("Asia/Kolkata");
echo date("h:i:sa");
?>
                            </textarea>
                                </div>
                            </div>
                    </fieldset>
                    <br>
                    <div class="form-group">

                        <label for="inputEmail" class="col-lg-3" style="font-size:16px;color:black;"><b>Classification(Tick the appropriate one, Leave blank if you find it difficulty):</b></label>
                        <div class="col-lg-9" name="checkbox">
                            <table>
                                <tr>
                                    <th>Nearmisscase</th>
                                    <th>First Aid Case</th>
                                    <th>Lost Time Injury</th>
                                    <th>Fatal</th>
                                    <th>Fire</th>
                                    <th>Emission/Discharge/Spill/Leak(Abnormal)</th>
                                    <th>Property Damage</th>
                                    <th>HIPO</th>
                                </tr>
                                <tr>
                                    <td><input type="checkbox" value="Nearmiss case" name="checkbox"></td>
                                    <td><input type="checkbox" value="First Aid Case" name="checkbox"></td>
                                    <td><input type="checkbox" value="Lost Time Injury" name="checkbox"></td>
                                    <td><input type="checkbox" value="Fatal" name="checkbox"></td>
                                    <td><input type="checkbox" value="Fire" name="checkbox"></td>
                                    <td><input type="checkbox" value="Emission/Discharge/Spill/Leak(Abnormal)" name="checkbox"></td>
                                    <td><input type="checkbox" value="Property Damage" name="checkbox"></td>
                                    <td><input type="checkbox" value="HIPO" name="checkbox"></td>
                                </tr>
                            </table>
                            <style>
                                table {
                                    font-family: sans-serif;
                                    border-collapse: collapse;
                                    width: 75.5%;
                                    font-size: 15px;
                                }

                                td,
                                th {
                                    border: 1px solid black;
                                    text-align: center;
                                    padding: 2px;
                                    font-weight: normal;
                                }

                            </style>
                        </div>
                    </div>
                    <br>

                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="font-size:17px;color:black;"><b>Details of Injured :</b></label>
                        <br>
                        <label for="inputEmail" class="col-lg-3"><b>Name:</b></label>
                        <div class="col-lg-9">
                            <textarea name="injured" cols="25" rows="1" class="form-control"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:20%; bottom:39px;">
                        <label for="inputEmail" class="col-lg-3"><b>Age/Gender:</b></label>
                        <div class="col-lg-9">
                            <textarea name="agegender" class="form-control" cols="3" rows="1"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:29%; bottom:78px;">
                        <label for="inputEmail" class="col-lg-3"><b>Contact:</b></label>
                        <div class="col-lg-9">
                            <textarea name="contact" class="form-control" cols="9" rows="1"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="position:absolute; left:1.2%; top:26.5%;">
                        <label for="inputEmail" class="col-lg-3"><b>Employee ID:</b></label>
                        <div class="col-lg-9">
                            <textarea name="empid" class="form-control" cols="3" rows="1"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:39%; bottom:117px;">
                        <label for="inputEmail" class="col-lg-3"><b>Organization:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="organization" class="form-control">
                        </div>
                    </div>

                    <div class="form-group" style="position:absolute; left:55%; bottom:74%;">
                        <label for="inputEmail" class="col-lg-3"><b>Department:</b></label>
                        <div class="col-lg-9">
                            <select name="dept" class="form-control">
                                <option value="">---------Select Dept--------</option>
                                <option value="MMD">MMD</option>
                                <option value="O&M">O&M</option>
                                <option value="Civil">Civil</option>
                                <option value="C&M">C&M</option>
                                <option value="Logistics">Logistics</option>
                                <option value="HR & ADMIN">HR & ADMIN</option>
                                <option value="Fire & Safety">Fire & Safety</option>
                                <option value="IT & MIS">IT & MIS</option>
                                <option value="F&M">F&M</option>
                                <option value="EMD">EMD</option>
                                <option value="C&I">C&I</option>
                                <option value="STORE">STORE</option>
                                <option value="EHS">EHS</option>
                                <option value="Tech Cell">Tech Cell</option>
                                <option value="Operation">Operation</option>
                                <option value="Chemist">Chemist</option>
                            </select>
                        </div>
                    </div><br><br>

                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:90px; font-size:17px;color:black;"><b>Summary Of Incident:</b></label>
                        <div class="col-lg-9" style="position:relative; bottom:90px;">
                            <textarea rows="8" cols="110" name="summary" class="form-control" maxlength="1000" onkeyup="textCounter(this,'counter3',1000);"></textarea><br><input style="position:absolute; left:72.5%;" disabled maxlength="3" size="3" value="1000" id="counter3">
                        </div>
                    </div>


                    <div class="form-group">
                        <label style="position:absolute; left:63%; top:425px;" for="inputEmail" class="col-lg-3"><b>Upload Images Here :</b></label><br><br>
                        <div class="col-lg-9">
                            
                            <input style="position:absolute; left:78%; top:420px;" type="file" name="name" enctype="multipart/form-data" class="form-control" name="incident_reference" onchange="document.getElementById('inc_ref').src = window.URL.createObjectURL(this.files[0]); document.getElementById('inc_ref').className +='_active'; document.getElementById('inc_ref_span').className += '_hidden'">
                        </div><iframe id="inc_ref" class="form-group" width="220px" height="130px" style="position:absolute; left:78%; top:32%;"></iframe>
                    </div>


                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:110px; font-size:17px;color:black;"><b>Potential outcome(Incase of Near Miss case or Potential Incident only):</b></label>
                        <div class="col-lg-9" style="position:relative; bottom:110px;">

                            <textarea rows="8" cols="110" name="outcome" class="form-control" maxlength="1000" maxlength="1000" onkeyup="textCounter(this,'counter2',1000);"></textarea><br><input style="position:absolute; left:76%; top:102px;" disabled maxlength="3" size="3" value="1000" id="counter2">
                        </div>
                    </div><br>
                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:110px; font-size:17px;color:black;"><b>Likely Cause:</b></label>
                        <div class="col-lg-9" style="position:relative; bottom:110px;">

                            <textarea rows="8" cols="110" name="cause" class="form-control" maxlength="1000" maxlength="1000" onkeyup="textCounter(this,'counter1',1000);"></textarea><br><input style="position:absolute; left:76%; top:102px;" disabled maxlength="3" size="3" value="1000" id="counter1">
                        </div>
                    </div><br>
                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:110px; font-size:17px;color:black;"><b>Immediate Action:</b></label>
                        <div class="col-lg-9" style="position:relative; bottom:110px;">

                            <textarea rows="8" cols="110" name="action" class="form-control" maxlength="1000" onkeyup="textCounter(this,'counter',1000);"></textarea><br><input style="position:absolute; left:76%; top:102px;" disabled maxlength="3" size="3" value="1000" id="counter">
                        </div>
                    </div><br>
                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:110px; font-size:17px;color:black;"><b>Incident reported by:</b></label>
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:80px; right:160px;"><b>Name:</b></label>
                        <div class="col-lg-9">
                            <input style="position:absolute; bottom:230px; left:10px;" type="text" name="reportedname" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:15%; bottom:100px;">
                        <label for="inputEmail" class="col-lg-3"><b>Position:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="position" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:31%; bottom:141px;">
                        <label for="inputEmail" class="col-lg-3"><b>Organisation:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="organisation" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:0%; bottom:130px;">
                        <label for="inputEmail" class="col-lg-3"><b>Contact:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="reportedcontact" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:175px; bottom:171px;">
                        <label for="inputEmail" class="col-lg-3"><b>Department:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="reporteddept" class="form-control">
                        </div>
                    </div>
                    <div class="col-lg-9">
                        <input type="hidden" name="status" class="form-control">
                    </div>

                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3"><b></b></label>
                        <div class="col-lg-9">
                            <input type="hidden" name="comment" class="form-control">
                        </div>
                    </div>
                </div>
            </fieldset>
            <br>
            <script type="text/javascript">
                function confSubmit(form) {
                    if (confirm("Are you sure you want to submit the form?")) {
                        form.submit();
                    } else {
                        alert("You decided to not submit the form!");
                    }
                }

            </script>
            <script>
                function textCounter(field, field2, maxlimit) {
                    var countfield = document.getElementById(field2);
                    if (field.value.length > maxlimit) {
                        field.value = field.value.substring(0, maxlimit);
                        return false;
                    } else {
                        countfield.value = maxlimit - field.value.length;
                    }
                }

            </script>
            <div class="form-group" style="position:absolute; left:33px;">
                <div class="col-lg-12">
                    <button class="button1" type="reset" class="btn btn-default">Cancel</button>
                    <button class="button1" type="submit" class="btn btn-primary" onClick="confSubmit(this.form);">Submit</button>
                    <style>
                        .button1:hover {
                            background-color: #555555;
                            color: aqua;
                            border: 3px solid grey;

                        }

                    </style>
                </div>
            </div>

        </form>
    </div>
</body>

</html>

Your input called “name” is a FILE type input, which means it will be in the $_FILES array, not the $_POST array.

<input style="position:absolute; left:78%; top:420px;" 
     type="file" name="name" enctype="multipart/form-data" 
     class="form-control" name="incident_reference" 
     onchange="document.getElementById('inc_ref').src = window.URL.createObjectURL(this.files[0]); document.getElementById('inc_ref').className +='_active'; document.getElementById('inc_ref_span').className += '_hidden'">

Now I read it again, though, you have two name attributes in there, so I’m not sure which will be used. Probably the latest one, which means you don’t have a field called name. I’m not sure it needs an enctype in there either, I thought that was just for the <form> tag.

All of your form labels seem to have for="inputEmail" on them - I thought that should refer to the form field that they are labelling, not all be the same.

Why do you use the textarea input type when you’re only inputting single row text entries? Surely an input type="text" would be more appropriate?

okay i have checked the input fields and found out that i have two name type which i have removed it and now it is working fine i can upload files in my directory and the name of it in my database as well. Thanks Sir for the help. :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.