I am trying to create a form where a user can upload images. I'm using php for validation of this file to see whether it is an image file or not but I am getting an error. The HTML code is as follows
<form name="myform" class="col s12" method="POST" action="registration.php" onsubmit="return validateform1()" enctype="multipart/form-data">
<input class="file-path validate" type="text" name="photo" id="photo" />
<button class="btn waves-effect waves-light" type="submit" name="submit">Submit
<i class="material-icons right">send</i>
</button>
</form>
PHP code is as follows. i had done for all the available options.. i think the varaible in html form is not accessing here or he image is isn't uploaded
<?php //start php tag
define ("MAX_SIZE","1000000");
$hostname="localhost"; //local server name default localhost
$username="root"; //mysql username default is root.
$password=""; //blank if no password is set for mysql.
$database="studentwelafare"; //database name which you created
$con=mysqli_connect($hostname,$username,$password);
if(! $con)
{
die('Connection Failed'.mysql_error());
}
mysqli_select_db($con,$database);
if(isset($_REQUEST['submit'])!='')
{
if(empty($_FILES) || !isset($_FILES['photo']))
{
$folderName = "upload/photo";
$validExt = array("jpg", "png", "jpeg", "bmp", "gif");
// $photo=null;
$photo=$_FILES['photo']['tmp_name']; **//error comes here at photo variable name**
If($_REQUEST['photo']='')
{
Echo "please fill the empty field.";
}
elseif ($_FILES["photo"]["size"] <=0) { **// error at "photo" variable**
echo "image is not proper";
}
else{
$ext=strtolower(end(explode(".", $photo)));
if (!in_array($ext, $validExt)) {
# code..
echo "not a valid image";
}
else{
$photo=$_REQUEST['photo'];
$filePath=$folderName.rand(10000,990000).'_'.time().'.'.$ext;
if(move_uploaded_file($_FILES["photo"]["tmp_name"], $filePath))
{
$sql1="INSERT INTO students(photo)VALUES('".prepare_input($filePath) ."')";
$res1=mysqli_query($con,$sql1);
if($res1)
{
Echo " Student Registerd successfully";
}
else
{
Echo "There is some problem in inserting record";
}
mysqli_close($con);
}
}
}
}
else{
echo "enter image";
}
}
?>