0

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>   
2
  • You should use move_uploaded_file() when dealing with uploaded files. Unfortunately many things may have failed in case of file upload. Read this subsection of PHP manual php.net/manual/en/features.file-upload.common-pitfalls.php and whole section titled Handling file uploads. Commented Feb 18, 2014 at 12:21
  • hi when i submit the output is this submitArray ( [upload] => Array ( [name] => omar.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\php3579.tmp [error] => 0 [size] => 262305 ) ) Commented Feb 18, 2014 at 14:58

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

hi thanks. but i tried to add enctype="multipart/form-data but no luck what do you think is my problem?
no error itemcode,itemname,brandname` are inserted but the upload image is not? why?
Well you should try starting debugging this stuff. Try to do var_dump on $_FILES and $_POST to see what's contents, and check if everything's alright
hi thanks i tried var_dump($_FILES['upload']); but the output is null?
just do var_dump of the $_FILES only, and if you don't see anything it means that yet your form have problems
|
1

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

1 Comment

hi i try to var_dump and this is the output submitArray ( [upload] => Array ( [name] => omar.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\php3579.tmp [error] => 0 [size] => 262305 ) )
0

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();

    ?>

1 Comment

thanks for your comment I add enctype="multipart/form-data" but doesn't work for me i think php file is the error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.