0

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";
}

 }
?> 
Qirel
  • 25,449
  • 7
  • 45
  • 62

3 Answers3

0

There are a couple of errors that might be affecting your code

if($_REQUEST['photo']=='') //--- lowercase i on if,  double equal
    {
    echo "please fill the empty field."; //--- lowercase e on echo
    }
else if ($_FILES["photo"]["size"] <=0) { // space between else if

    echo "image is not proper";
}
0

In your html code input type is not "file". So You can not get data in $_FILES["photo"];

if you want then change your input type to file.

B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

Update your code with this following code:

<form name="myform" class="col s12" method="POST" action="registration.php" onsubmit="return validateform1()" enctype="multipart/form-data">
            <input class="file-path validate" type="file" 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>

and for registration.php:

<?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']['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('" . mysqli_real_escape_string($con, $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";
    }
}
?>

Wish that will help

Thanks.

srromy
  • 15
  • 3