user3323106
user3323106

Reputation: 1

Failed to upload a file in PHP

I try to upload an image, but it is not working. Other variables I have set are inserted into database, but image file is not... I was trying to check submit with isset, but it is not working. Where is my error?

Thanks for your help.

PHP file:

<?php

include ('includes/config.php'); 
$mysqli =  new mysqli(DB_SERVER,DB_UNAME,DB_PASSWD,DB_NAME);
if($mysqli->connect_errno) {
    echo "MYSQLI connect error no {$mysqli->connect_errno} : {$mysqli->connect_error}";
    die();
}

$itemcode  = $_POST['icode'];
$itemname  = $_POST['iname'];
$brandname = $_POST['brandname'];
$upload    = basename ($_FILES['upload']['name']);
$path = "img/";

if(!empty($upload)) {
    $i1 = strrpos($upload,".");
    if (!$i1) { 
        return ""; 
    }
    $l1 = strlen($upload) - $i1;
    $ext1 = substr($upload,$i1+1,$l1);
    $ext1 = strtolower($ext1);
    $news_name1=time()+(1).'.'.$ext1;
    $newname1 = $path.$news_name1;
    $copied1 = copy($_FILES['upload']['tmp_name'], $newname1);
} else {
    $news_name1 = '';
}

$iadd = $mysqli->prepare("INSERT INTO table_item (`itemcode`,`itemname`,`brandname`,`upload`) VALUES ('".$itemcode."', '".$itemname."','".$brandname."','".$news_name1."')  ");
$iadd->execute();
$iadd->close();
$mysqli->close();

?>

This is my HTML file:

  <form class="cmxform form-horizontal tasi-form" name="form2" id="form2" method="post" action="">
      <div class="form-group ">
      <label for="icode" class="control-label col-lg-2">Item Code</label>
          <div class="col-lg-10">
              <input class=" form-control" id="icode" name="icode" type="text" />
          </div>
      </div>
      <div class="form-group ">
          <label for="iname" class="control-label col-lg-2">Item Name</label>
          <div class="col-lg-10">
              <input class=" form-control" id="iname" name="iname" type="text" />
          </div>
      </div>                                
      <div class="form-group ">
          <label for="brandname" class="control-label col-lg-2">Brand Name</label>
          <div class="col-lg-10">
              <input class=" form-control" id="brandname" name="brandname" type="text" />
          </div>
      </div>
      <fieldset style="width:48%; float:left;"> <!-- to make two field float next to one another, adjust values accordingly -->
          <label>Doc 2</label>
          <input style="margin: 0 10px;" type="file" name="upload" size="50">
      </fieldset>   

Upvotes: 0

Views: 89

Answers (3)

SagarPPanchal
SagarPPanchal

Reputation: 10121

You have not proper syntax used and also use 'enctype="multipart/form-data"'. I have implemented your code

<?php

    include ('includes/config.php');

    $mysqli =  new mysqli(DB_SERVER,DB_UNAME,DB_PASSWD,DB_NAME);

    if($mysqli->connect_errno){

        echo "MYSQLI connect error no {$mysqli->connect_errno} : {$mysqli->connect_error}";
        die();
    }
    $itemcode  = $_POST['icode'];
    $itemname  = $_POST['iname'];
    $brandname = $_POST['brandname'];
    $upload    = basename ($_FILES['upload']['name']);
    $path = "img/";

      if(!empty($upload)){
            $i1 = strrpos($upload,".");
            if (!$i1) { return ""; }
            $l1 = strlen($upload) - $i1;
            $ext1 = substr($upload,$i1+1,$l1);
            $ext1 = strtolower($ext1);
            $news_name1=time()+(1).'.'.$ext1;
            $newname1 = $path.$news_name1;
            $copied1 = $_FILES['upload']['tmp_name'], $newname1;
        }else{
            $news_name1 = '';
        }

    $iadd = $mysqli->prepare("INSERT INTO table_item (`itemcode`,`itemname`,`brandname`,`upload`) VALUES ('".$itemcode."', '".$itemname."','".$brandname."','".$news_name1."')  ");
    $iadd->execute();
    $iadd->close();
    $mysqli->close();

    ?>

Upvotes: 0

PravinS
PravinS

Reputation: 2584

add form attribute enctype="multipart/form-data"

Upvotes: 1

Michael Arenzon
Michael Arenzon

Reputation: 551

Add 'enctype="multipart/form-data"' to your form tag attributes, you can read more about file uploading here. Also consider checking the values of the post, because your current method can get you sql injections

Upvotes: 1

Related Questions