-1

I'm having an Issue while trying to upload an image and status update. If the user doesn't want to upload an image along the side of their status it will insert perfectly fine into the table (Newsfeed) but if they want to add an image along with it, it comes up with an error : Undefined index: img in .... Any ideas on how I can fix this?

Heres my HTML where they can upload an image along with a status update.

<form action="include/updatestatus.php" method="post">
 <div class="form-group">
  <label for="status">Status</label>
  <input type="text" class="form-control" name="status" id="status" placeholder="<?php echo $status;?>">
</div>
<!-- Single button -->
<p ><span class="glyphicon glyphicon-film"></span> Upload an Image?:
  <input type="file" name="img" class="btn btn-default">
</p>
<div class="text-right">
        <button type="submit" name="statusupdate" class="btn btn-default btn-md">
          <span class="glyphicon glyphicon-thumbs-up"></span> Update Status
        </button>
      </div>
</form>

Heres the upload and insert php code. (Include/updatestatus.php)

<?php
if (isset($_POST['statusupdate'])) {
    $date = date("Y.m.d");
//If Post Image is blank --> insert with nothing.
if (empty($_POST['img'])) {
    // prepare sql and bind parameters
    $stmt = $DB_con->prepare("INSERT INTO newsfeed (userid, status,uploaddate)
    VALUES (:userid, :status, :date)");
    $stmt->bindParam(':userid', $_SESSION['user']['id']);
    $stmt->bindParam(':status', $_POST['status']);
     $stmt->bindParam(':date', $date);
    $stmt->execute();

    echo "New records created successfully";
    }
else {

 $name=$_FILES['img']['name'];
 $type=$_FILES['img']['type'];
 $size=($_FILES['img']['size'])/1024;

 $ext=end(explode('.',$name));
 if (($ext == "gif")
 || ($ext == "jpeg")
 || ($ext == "jpg")
 || ($ext =="png")
 || ($ext =="PNG")
 && ($size > 50))
 {
 //Changes Name to Number :)
 $newname=uniqid();
 $imagename=$newname.".".$ext;
 $directory="images/upload/";
 $fulldirectory=$directory.$imagename;
 if(move_uploaded_file($_FILES['img']['tmp_name'],$fulldirectory))
 {
   $stmt = $DB_con->prepare("INSERT INTO newsfeed (userid, status,uploaddate,image)
   VALUES (:userid, :status, :date, :image)");
       $stmt->bindParam(':userid', $_SESSION['user']['id']);
       $stmt->bindParam(':status', $_POST['status']);
      $stmt->bindParam(':date', $date);
      $stmt->bindParam(':image', $imagename);


 //If upload is a success --> Redirect to profile.php?action=upload.success
       if($stmt->execute()){
   header ('location: ../profile.php?action=upload.success');

       }
     else
       header ('location: ../profile.php?action=upload.failed');
     }
 else
 {
    header('location: ../profile.php?action=file.size');
 }
 }
 else{
     header('location: ../profile.php?action=wrong.extension');
     }
   } //end of first else
} //end of $_Post['statusupdate']
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

2 Answers2

0

Try to add enctype="multipart/form-data" into tag.

To check if file was uploaded use the following:

if(!file_exists($_FILES['img']['tmp_name']) || !is_uploaded_file($_FILES['img']['tmp_name'])) {
    echo 'No upload';
}
vich
  • 262
  • 2
  • 15
0

your form tag must include enctype="multipart/form-data" for a file upload.

also you check for $_POST['img'] which will never exist, as its the $_FILES array that contains the image data