1

I have the code below and I can't seem to figure out why my query is failing. Do anyone have an idea why?
I have tried many suggested solutions that I found online but none of them worked.

The HTML form code follows the PHP code

<?php include 'connection.php';?>

<?php
    $dir           = substr(uniqid(),-7);
    $valid_formats = array("jpg", "png", "gif", "jpeg");
    $max_file_size = 1024*100; //100 kb

    /*
       $path = "Prototype/uploads/"; // Upload directory
       mkdir ($path, 0744);\
    */

    $count = 0;

    if (isset($_POST['search'])) {
        // Loop $_FILES to exeicute all files
        foreach ($_FILES['files']['name'] as $f => $name) {   
            echo "$name--";
            if ($_FILES['files']['error'][$f] == 4) {
                continue; // Skip file if any error found
                echo "something <br>";
            }          

            if ($_FILES['files']['error'][$f] == 0) {              
                if ($_FILES['files']['size'][$f] > $max_file_size) {
                    $message[] = "$name is too large!.";
                    echo "something***************** <br>";
                    continue; // Skip large files
                } elseif (! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
                    $message[] = "$name is not a valid format";
                    echo "something+++++++++++++++++++ <br>";
                    echo "$name-- ";
                    continue; // Skip invalid file formats
                } else { // No error found! Move uploaded files 
                    // if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                    // $count=$count+1; // Number of successfully uploaded file             
                    //echo $path.$name;

                    $image = addslashes(file_get_contents($_FILES['files']['tmp_name'][$f]));
                    $image_name = addslashes($_FILES['files']['name'][$f]);
                    $query2 = "Insert into $dbname.Image (Image, ImageName) VALUES ('$image', '$image_name')";
                    $result2 = mysqli_query($conn,$query2);

                    if (!$result2) {
                        echo "ERRORS";
                    }

                    //Number of successfully uploaded file

                }
            }
        }
        echo "$count files were imported";
    }

    //show success message
    /*
        echo "<h1>Uploaded:</h1>";    

        if(is_array($files)){
            echo "<ul>";
            foreach($files as $file){
                echo "<li>$file</li>";
            }
            echo "</ul>";
        }
    */
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
  <label class="btn btn-primary" for="my-file-selector">
    <input id="my-file-selector" type="file"  name="files[]" style="display:none;" multiple onchange="$('#upload-file-info').html($(this).val());">
    Browse
  </label>
  <span class='label label-info' id="upload-file-info"></span>
  <div style="float:right;">
    <label class="btn btn-primary" for="my-file-selector2">
      <input id="my-file-selector2" type="Submit" style="display:none;" name="search">
      Save
    </label>
  </div>
</form>
phaberest
  • 3,140
  • 3
  • 32
  • 40
Bobby
  • 496
  • 5
  • 18

2 Answers2

0

I suggest you to not to save images directly to database.

Rather you can either move the files to cloud services like S3 and store that url in database.

if S3 is not your choice, Please upload the image to any folder in the same project and store that url.

Is there any specific reason to store the binary image in database ?

0

Try this:-

<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Multiple File Ppload with PHP</title>
</head>
<body>
  <form action="" method="post" enctype="multipart/form-data">
    <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
  <input type="submit" value="Upload!" />
</form>
</body>
</html>

$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to exeicute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; // Number of successfully uploaded file
            }
        }
    }
}

link:-Multiple Image Upload PHP form with one input

Community
  • 1
  • 1
Razia sultana
  • 2,168
  • 3
  • 15
  • 20