我想在一个文件夹中插入3个图像,并使用新名称将路径导入mysql

时间:2018-04-21 05:04:47

标签: php mysql image file-upload

我想将3个图像插入到一个文件夹中,并使用新名称插入到mysql中。

我的表名是' ict'与数据库' ctg'。表字段是id,image1,image2,image3。

现在我想使用一个包含三个输入类型="文件"和一个提交按钮。

PHP怎么可能?

感谢。

1 个答案:

答案 0 :(得分:0)

# Below Database.php 

<?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "ctg";



    // Create connection
    connecttodb($servername,$dbname,$username,$password);
    function connecttodb($servername,$dbname,$dbuser,$dbpassword)
    {
        global $link;

        $link=mysql_connect ("$servername","$dbuser","$dbpassword");

        if(!$link){
                die("Could not connect to MySQL");
        }

        mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); 
    }
    ?>

# Below File.php

    <?php
//error_reporting(0);

include("database.php");

 if(isset($_POST['upload']))
{
    /* Concept Image */

    $aMyUploads = array();
    foreach ($_FILES as $key => $aFile) {
        for($i = 0; $i<count($aFile['error']); $i++){
            //echo $aFile['error'][$i]; exit;
            if(0 === $aFile['error'][$i]){
                    $uploadLocation = 'uploads/'.$aFile['name'][$i];
            }   
            if(0 === $aFile['error'][$i] && (false !== move_uploaded_file($aFile['tmp_name'][$i], $uploadLocation))){
                $aMyUploads[] = $uploadLocation;
            } else {
                $aMyUploads[] = '';
            }
        }   
    }

    if(is_array($aMyUploads)){
        $image1 = $aMyUploads[0]; 
        $image2 = $aMyUploads[1];
        $image3 = $aMyUploads[2];
        $qry = mysql_query("INSERT INTO `ict` (`image1`, `image2`, `image3`) VALUES ('$image1', '$image2', '$image3')");
        //$ex =mysql_query($qry,$con);
    }else{
        echo "ERROR :: Not insert Please try";
    }
}
?>
<html>
 <form method="post" enctype="multipart/form-data" class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <div class="form-group">
      <label class="control-label col-sm-4">IMAGE1</label>
      <div class="col-sm-10 col-md-offset-0 col-md-4">
      <input type="file" class="form-control" name="upload_files[]">
      </div> 
    </div>
    <div class="form-group">
      <label class="control-label col-sm-4">IMAGE2</label>
      <div class="col-sm-10 col-md-offset-0 col-md-4">
      <input type="file" class="form-control" name="upload_files[]">
      </div> 
    </div>
    <div class="form-group">
      <label class="control-label col-sm-4">IMAGE3</label>
      <div class="col-sm-10 col-md-offset-0 col-md-4">
      <input type="file" class="form-control" name="upload_files[]">
      </div> 
    </div>
    <div class="col-sm-10 col-md-7 col-md-offset-4">
      <button type="submit" name="upload">Upload</button>
    </div> 
    </form>
</html>