0

I am uploading multiple images via the code below (it works), but I can't insert these images' urls into my database. I tried a few thing but can't make it work. My code is inn't completed yet, so I didn't add controls (filetype, filesize, etc) yet.

index.php

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    <div id="file_container">
        <input name="images[]" multiple type="file" id="file[]"/><br/>
        <input type="submit">
    </div>
</form>

upload.php

$target = "upload/";
$test = 1;

foreach ($_FILES['images']['name'] as $key => $value) {
    $path = $_FILES['images']['name'][$key];
    $ext = pathinfo($path, PATHINFO_EXTENSION); // getting the extension

    // creating a unique value here
    $name = md5($name);
    $generate1 = md5(date('Y-m-d H:i:s:u'));
    $randomizer = uniqid($name);
    $name = $name . $generate1 . $randomizer;
    $makeaname = $target . $name . "." . $ext;

    if ($test == 1) {
        if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $makeaname)) {
            echo "<strong>" . $value . "</strong> successful <br />\n";
            echo $makeaname; // it echoes image urls, so everything is okay so far.
        }
    } else {
        echo "Failed";
    }

I used the query below in a foreach loop after echo $makeaname; but it didn't work. I appreciate any kind of help or guidance.

$upload_image = $sqli->prepare("INSERT INTO images(image_value, type, size) VALUES (?,?,?)");
$upload_image->bind_param("sss", $makeaname, $_FILES['images']['type'], $_FILES['images']['size']);
$upload_image->execute();
3
  • 1
    $makeaname is a string of the filename, not the actual file. first, you need to modify your bind_param call from "sss" to be "bss" and second you need to supply the actual file. see blogs.oracle.com/oswald/entry/php_s_mysqli_extension_storing Commented Jan 1, 2015 at 0:09
  • Have you checked for error messages in your query? Also Is this $_FILES['images']['type'] correct in your bind statement?` In other words, should it be $_FILES['images']['type'][$key]? Commented Jan 1, 2015 at 0:10
  • 1
    @Gohn67, thank you. Now size and type columns can be added perfectly. I also noticed that I forgot to include connect.php file at the top of my file, which was the dumbest thing I've have ever done in 2015. I congratulate myself on that. Commented Jan 1, 2015 at 0:30

2 Answers 2

1

You need to index the $_FILES arrays. Also, you don't need to bind a parameter for the NULL value, use a NULL literal in the SQL (or you can just leave the value out entirely if this is the default in the schema).

upload_image = $sqli->prepare("INSERT INTO images(image_value, type, size) VALUES (NULL,?,?)");
$upload_image->bind_param("ss", $type, $size);
foreach ($_FILES['images']['type'] as $i => $type) {
    $size = $_FILES['images']['size'][$i];
    $upload_image->execute();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I already solved my issue but indexing the $_FILES array is a thing I learned here. Thanks. (You can read my comment above, the last one on the question)
0

Make sure that your database column "image_value" is a BLOB data type. Then try this code instead:

$null = NULL;
$upload_image = $sqli->prepare("INSERT INTO images(image_value, type, size) VALUES (?,?,?)");
$upload_image->bind_param("bss", $null, $_FILES['images']['type'], $_FILES['images']['size']);
$upload_image->send_long_data(0, file_get_contents($makeaname));
$upload_image->execute();

1 Comment

Please don't get me wrong, but I can't make it work. It gives me an array - string conversion error. I am working on it, though. Thank you for your time.

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.