How to upload multiple images in php and store it as blob mysql












0















Here is my current code and can anyone help me how i can upload multiple images with a text in category? i have to store the images as blob in database.



<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("au");
$cat = $_POST['cat'];
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData, category)
VALUES('{$imageProperties['mime']}', '{$imgData}', '$cat')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input type="text" name="cat">
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>









share|improve this question

























  • >> Possible Duplicate

    – Karlo Kokkak
    Apr 28 '18 at 8:54






  • 1





    If the images are more than about 100k, then this isn't such a great idea

    – Strawberry
    Apr 28 '18 at 9:04
















0















Here is my current code and can anyone help me how i can upload multiple images with a text in category? i have to store the images as blob in database.



<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("au");
$cat = $_POST['cat'];
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData, category)
VALUES('{$imageProperties['mime']}', '{$imgData}', '$cat')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input type="text" name="cat">
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>









share|improve this question

























  • >> Possible Duplicate

    – Karlo Kokkak
    Apr 28 '18 at 8:54






  • 1





    If the images are more than about 100k, then this isn't such a great idea

    – Strawberry
    Apr 28 '18 at 9:04














0












0








0








Here is my current code and can anyone help me how i can upload multiple images with a text in category? i have to store the images as blob in database.



<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("au");
$cat = $_POST['cat'];
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData, category)
VALUES('{$imageProperties['mime']}', '{$imgData}', '$cat')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input type="text" name="cat">
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>









share|improve this question
















Here is my current code and can anyone help me how i can upload multiple images with a text in category? i have to store the images as blob in database.



<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("au");
$cat = $_POST['cat'];
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData, category)
VALUES('{$imageProperties['mime']}', '{$imgData}', '$cat')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input type="text" name="cat">
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>






php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 28 '18 at 8:54









GYaN

2,09731431




2,09731431










asked Apr 28 '18 at 8:51









Jack LarsonJack Larson

216




216













  • >> Possible Duplicate

    – Karlo Kokkak
    Apr 28 '18 at 8:54






  • 1





    If the images are more than about 100k, then this isn't such a great idea

    – Strawberry
    Apr 28 '18 at 9:04



















  • >> Possible Duplicate

    – Karlo Kokkak
    Apr 28 '18 at 8:54






  • 1





    If the images are more than about 100k, then this isn't such a great idea

    – Strawberry
    Apr 28 '18 at 9:04

















>> Possible Duplicate

– Karlo Kokkak
Apr 28 '18 at 8:54





>> Possible Duplicate

– Karlo Kokkak
Apr 28 '18 at 8:54




1




1





If the images are more than about 100k, then this isn't such a great idea

– Strawberry
Apr 28 '18 at 9:04





If the images are more than about 100k, then this isn't such a great idea

– Strawberry
Apr 28 '18 at 9:04












1 Answer
1






active

oldest

votes


















0














I have two articles in my blog refer to multiple files uploading and storing images in MySQL database.




  • Multiple files uploading


upload.html



<form action="product.php" method="post" enctype="multipart/form-data">
<input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
<input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
:
<button type="submit" class="btn btn-primary">Save Prodcut Info</button>
</form>


Note that the 'name' property of the <input> tag should be added an array tag ''. In addition, enctype="multipart/form-data" should be added in the tag in order to enable the file uploading function. Thus, the $_FILES variable can be an array to carry multiple files information.



Then, the product.php converts $_FILE variable and save files to the destination path.



product.php



<?php
if (isset($_FILES['prod_pic'])) {
$pics = convert_upload_file_array($_FILES['prod_pic']);

foreach ($pics as $key => $pic) {
$target = "images/{$pic['name']}";
move_uploaded_file($pic['tmp_name'], $target);
}
}
?>


$_FILES variable as an array is not as a usual form as we think. It is formed by using file’s attribute as the key, not the index number. Therefore, we need a converting function convert_upload_file_array().



function convert_upload_file_array($upload_files) {
$converted = array();

foreach ($upload_files as $attribute => $val_array) {
foreach ($val_array as $index => $value) {
$converted[$index][$attribute] = $value;
}
}
return $converted;
}


Read more: How to Upload Multiple Files in PHP




  • Regarding storing images in MySQL database


Please refer to the following code:



$prod_pic = $mysqli->real_escape_string(file_get_contents($_FILES['prod_pic']['tmp_name']));
$prod_pic_type = $_FILES['prod_pic']['type'];
:
:
$sql = "UPDATE products
SET prod_pic = '{$prod_pic}',
prod_pic_type = '{$prod_pic_type}'
WHERE prod_id = {$prod_id}";
$mysqli->query($sql) or die($mysqli->connect_error);
$mysqli->close();


The key part is As you can see, the key part is:



file_get_contents($_FILES['prod_pic']['tmp_name'])


and



$mysqli->real_escape_string(......)


Read more: Insert and Update An Image to MySQL Server Field with Blob Data Type in PHP



By using the key ideas above, your goal should be easily achieved.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50074676%2fhow-to-upload-multiple-images-in-php-and-store-it-as-blob-mysql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have two articles in my blog refer to multiple files uploading and storing images in MySQL database.




    • Multiple files uploading


    upload.html



    <form action="product.php" method="post" enctype="multipart/form-data">
    <input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
    <input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
    :
    <button type="submit" class="btn btn-primary">Save Prodcut Info</button>
    </form>


    Note that the 'name' property of the <input> tag should be added an array tag ''. In addition, enctype="multipart/form-data" should be added in the tag in order to enable the file uploading function. Thus, the $_FILES variable can be an array to carry multiple files information.



    Then, the product.php converts $_FILE variable and save files to the destination path.



    product.php



    <?php
    if (isset($_FILES['prod_pic'])) {
    $pics = convert_upload_file_array($_FILES['prod_pic']);

    foreach ($pics as $key => $pic) {
    $target = "images/{$pic['name']}";
    move_uploaded_file($pic['tmp_name'], $target);
    }
    }
    ?>


    $_FILES variable as an array is not as a usual form as we think. It is formed by using file’s attribute as the key, not the index number. Therefore, we need a converting function convert_upload_file_array().



    function convert_upload_file_array($upload_files) {
    $converted = array();

    foreach ($upload_files as $attribute => $val_array) {
    foreach ($val_array as $index => $value) {
    $converted[$index][$attribute] = $value;
    }
    }
    return $converted;
    }


    Read more: How to Upload Multiple Files in PHP




    • Regarding storing images in MySQL database


    Please refer to the following code:



    $prod_pic = $mysqli->real_escape_string(file_get_contents($_FILES['prod_pic']['tmp_name']));
    $prod_pic_type = $_FILES['prod_pic']['type'];
    :
    :
    $sql = "UPDATE products
    SET prod_pic = '{$prod_pic}',
    prod_pic_type = '{$prod_pic_type}'
    WHERE prod_id = {$prod_id}";
    $mysqli->query($sql) or die($mysqli->connect_error);
    $mysqli->close();


    The key part is As you can see, the key part is:



    file_get_contents($_FILES['prod_pic']['tmp_name'])


    and



    $mysqli->real_escape_string(......)


    Read more: Insert and Update An Image to MySQL Server Field with Blob Data Type in PHP



    By using the key ideas above, your goal should be easily achieved.






    share|improve this answer




























      0














      I have two articles in my blog refer to multiple files uploading and storing images in MySQL database.




      • Multiple files uploading


      upload.html



      <form action="product.php" method="post" enctype="multipart/form-data">
      <input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
      <input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
      :
      <button type="submit" class="btn btn-primary">Save Prodcut Info</button>
      </form>


      Note that the 'name' property of the <input> tag should be added an array tag ''. In addition, enctype="multipart/form-data" should be added in the tag in order to enable the file uploading function. Thus, the $_FILES variable can be an array to carry multiple files information.



      Then, the product.php converts $_FILE variable and save files to the destination path.



      product.php



      <?php
      if (isset($_FILES['prod_pic'])) {
      $pics = convert_upload_file_array($_FILES['prod_pic']);

      foreach ($pics as $key => $pic) {
      $target = "images/{$pic['name']}";
      move_uploaded_file($pic['tmp_name'], $target);
      }
      }
      ?>


      $_FILES variable as an array is not as a usual form as we think. It is formed by using file’s attribute as the key, not the index number. Therefore, we need a converting function convert_upload_file_array().



      function convert_upload_file_array($upload_files) {
      $converted = array();

      foreach ($upload_files as $attribute => $val_array) {
      foreach ($val_array as $index => $value) {
      $converted[$index][$attribute] = $value;
      }
      }
      return $converted;
      }


      Read more: How to Upload Multiple Files in PHP




      • Regarding storing images in MySQL database


      Please refer to the following code:



      $prod_pic = $mysqli->real_escape_string(file_get_contents($_FILES['prod_pic']['tmp_name']));
      $prod_pic_type = $_FILES['prod_pic']['type'];
      :
      :
      $sql = "UPDATE products
      SET prod_pic = '{$prod_pic}',
      prod_pic_type = '{$prod_pic_type}'
      WHERE prod_id = {$prod_id}";
      $mysqli->query($sql) or die($mysqli->connect_error);
      $mysqli->close();


      The key part is As you can see, the key part is:



      file_get_contents($_FILES['prod_pic']['tmp_name'])


      and



      $mysqli->real_escape_string(......)


      Read more: Insert and Update An Image to MySQL Server Field with Blob Data Type in PHP



      By using the key ideas above, your goal should be easily achieved.






      share|improve this answer


























        0












        0








        0







        I have two articles in my blog refer to multiple files uploading and storing images in MySQL database.




        • Multiple files uploading


        upload.html



        <form action="product.php" method="post" enctype="multipart/form-data">
        <input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
        <input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
        :
        <button type="submit" class="btn btn-primary">Save Prodcut Info</button>
        </form>


        Note that the 'name' property of the <input> tag should be added an array tag ''. In addition, enctype="multipart/form-data" should be added in the tag in order to enable the file uploading function. Thus, the $_FILES variable can be an array to carry multiple files information.



        Then, the product.php converts $_FILE variable and save files to the destination path.



        product.php



        <?php
        if (isset($_FILES['prod_pic'])) {
        $pics = convert_upload_file_array($_FILES['prod_pic']);

        foreach ($pics as $key => $pic) {
        $target = "images/{$pic['name']}";
        move_uploaded_file($pic['tmp_name'], $target);
        }
        }
        ?>


        $_FILES variable as an array is not as a usual form as we think. It is formed by using file’s attribute as the key, not the index number. Therefore, we need a converting function convert_upload_file_array().



        function convert_upload_file_array($upload_files) {
        $converted = array();

        foreach ($upload_files as $attribute => $val_array) {
        foreach ($val_array as $index => $value) {
        $converted[$index][$attribute] = $value;
        }
        }
        return $converted;
        }


        Read more: How to Upload Multiple Files in PHP




        • Regarding storing images in MySQL database


        Please refer to the following code:



        $prod_pic = $mysqli->real_escape_string(file_get_contents($_FILES['prod_pic']['tmp_name']));
        $prod_pic_type = $_FILES['prod_pic']['type'];
        :
        :
        $sql = "UPDATE products
        SET prod_pic = '{$prod_pic}',
        prod_pic_type = '{$prod_pic_type}'
        WHERE prod_id = {$prod_id}";
        $mysqli->query($sql) or die($mysqli->connect_error);
        $mysqli->close();


        The key part is As you can see, the key part is:



        file_get_contents($_FILES['prod_pic']['tmp_name'])


        and



        $mysqli->real_escape_string(......)


        Read more: Insert and Update An Image to MySQL Server Field with Blob Data Type in PHP



        By using the key ideas above, your goal should be easily achieved.






        share|improve this answer













        I have two articles in my blog refer to multiple files uploading and storing images in MySQL database.




        • Multiple files uploading


        upload.html



        <form action="product.php" method="post" enctype="multipart/form-data">
        <input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
        <input type="file" class="form-control-file" name="prod_pic" id="prod_pic">
        :
        <button type="submit" class="btn btn-primary">Save Prodcut Info</button>
        </form>


        Note that the 'name' property of the <input> tag should be added an array tag ''. In addition, enctype="multipart/form-data" should be added in the tag in order to enable the file uploading function. Thus, the $_FILES variable can be an array to carry multiple files information.



        Then, the product.php converts $_FILE variable and save files to the destination path.



        product.php



        <?php
        if (isset($_FILES['prod_pic'])) {
        $pics = convert_upload_file_array($_FILES['prod_pic']);

        foreach ($pics as $key => $pic) {
        $target = "images/{$pic['name']}";
        move_uploaded_file($pic['tmp_name'], $target);
        }
        }
        ?>


        $_FILES variable as an array is not as a usual form as we think. It is formed by using file’s attribute as the key, not the index number. Therefore, we need a converting function convert_upload_file_array().



        function convert_upload_file_array($upload_files) {
        $converted = array();

        foreach ($upload_files as $attribute => $val_array) {
        foreach ($val_array as $index => $value) {
        $converted[$index][$attribute] = $value;
        }
        }
        return $converted;
        }


        Read more: How to Upload Multiple Files in PHP




        • Regarding storing images in MySQL database


        Please refer to the following code:



        $prod_pic = $mysqli->real_escape_string(file_get_contents($_FILES['prod_pic']['tmp_name']));
        $prod_pic_type = $_FILES['prod_pic']['type'];
        :
        :
        $sql = "UPDATE products
        SET prod_pic = '{$prod_pic}',
        prod_pic_type = '{$prod_pic_type}'
        WHERE prod_id = {$prod_id}";
        $mysqli->query($sql) or die($mysqli->connect_error);
        $mysqli->close();


        The key part is As you can see, the key part is:



        file_get_contents($_FILES['prod_pic']['tmp_name'])


        and



        $mysqli->real_escape_string(......)


        Read more: Insert and Update An Image to MySQL Server Field with Blob Data Type in PHP



        By using the key ideas above, your goal should be easily achieved.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 9:16









        Charles HsuCharles Hsu

        11




        11
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50074676%2fhow-to-upload-multiple-images-in-php-and-store-it-as-blob-mysql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy