Image insert like medium blob from page in mysql

I thought you weren’t a beginner? Nevertheless http://php.net/manual/en/function.move-uploaded-file.php

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        // basename() may prevent filesystem traversal attacks;
        // further validation/sanitation of the filename may be appropriate
        $name = basename($_FILES["pictures"]["name"][$key]);
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs

You’ve got to stop copying and pasting whatever you see. I gave you the link to move_uploaded_file because that’s the function you have to use to upload files. I don’t think you understand what I am trying to get at when I give you these hints.

I am not going to do your work for you. You have to do it yourself. I will kindly guide you in the right path as I am doing now. But I am not going to just do it for you. So please stop copying and pasting and read what I give you first. The conduct some thinking process and figure out what you really need from what I give you.

If you just keep copying and pasting, you are going to keep having tons of errors and problems because almost all codes on the internet is not built specifically for you.

1 Like

I have been making this for 2 days i had problem solved but images blob is not good .Now i have text images names in db now i need to put on localhost server to display them .I i am little tired of this because i need to guide me faster.

The speed and pace is up to you. Whether you want to learn is up to you. If all you do is copy and paste, it’s going to be longer than 2 days. I am only going to guide you as fast as you are going to learn. If you learn slow, then I’ll guide you slow. If you learn fast, then I’ll guide you fast.

<?php



// Initialize message variable
$msg = "";

if (isset($_POST['upload'])) {

  $images = $_FILES['images']['name'];

  $target = "images/".basename($images);

  $sql = "INSERT INTO blog1 (images) "
          . "VALUES ('$images')";

  mysqli_query($db, $sql);

  if (move_uploaded_file($_FILES['images']['tmp_name'], $target)) {
      $msg = "Image uploaded successfully";
  }else{
      $msg = "Failed to upload image";
  }
}

$result = mysqli_query($db, "SELECT * FROM images");
?>

<form id='imageForm' method="POST" action="" enctype="multipart/form-data">
      <input type="hidden" name="size" value="1000000">
   <div>
       <input class="file-upload" type="file" name='images' accept='images/*' />
   </div>
   <div class="upload-button">
       <button class="" name='upload'>UPLOAD</button>
   </div>
</form>


:smiley:I sloved a problem

Good. You see what you get without copying and pasting? Now solve the gapping security hole you have there.

1 Like

You should first run move_uploaded_file and then mysqli_query. If you run mysqli_query first and move_uploaded_file fails there is a record in the database pointing to an image that doesn’t exist.

Or, even better, run the entire thing in a transaction:

$sql = "INSERT INTO blog1 (images) "
          . "VALUES ('$images')";

mysqli_begin_transaction($db);
mysqli_query($db, $sql);

if (move_uploaded_file($_FILES['images']['tmp_name'], $target)) {
    $msg = "Image uploaded successfully";
    mysqli_commit($db);
} else {
    $msg = "Failed to upload image";
    mysqli_rollback($db);
}

(only works if you use InnoDB tables, not if you use MyISAM tables - run SHOW CREATE TABLE blog1 to see which engine it’s using)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.