如何将多个图像上传到PHP和Mysql中

时间:2017-11-08 05:43:52

标签: php html

大家好,
我有一个由10列组成的表,其中4列我想插入图像路径。
任何人都可以指导我如何将图像路径与其他6个列数据一起插入数据库,并将图像上传到服务器(文件夹内)。

这里是我的HTML代码:

<div class="panel-body">
<div class="form-group">
    <label style="align-content:center" for="inputdefault">Product full name</label>
    <input class="form-control" id="inputdefault" name="name2" type="text">
</div>

<div class="panel-body">
<div class="form-group">
    <label style="align-content:center" for="inputdefault">Product category</label>
    <input class="form-control" id="inputdefault2" name="name1" type="text">
</div>

<div class="panel-body">
<div class="form-group">
    <label style="align-content:center" for="inputdefault">Product qty</label>
    <input class="form-control" id="inputdefault3" name="name3" type="text">
</div>

<div class="panel-body">
<div class="form-group">
    <label style="align-content:center" for="inputdefault">Picture 1.</label>
    <input type="file" id="file3" name="files[]" multiple="multiple" accept="image/*" />
</div>
<div class="form-group">
    <label style="align-content:center" for="inputdefault">Picture 2.</label>
    <input type="file" id="file3" name="files[]" multiple="multiple" accept="image/*" />
</div>
<div class="form-group">
    <label style="align-content:center" for="inputdefault">Picture 3.</label>
    <input type="file" id="file3" name="files[]" multiple="multiple" accept="image/*" />
</div>
<div class="form-group">
    <label style="align-content:center" for="inputdefault">Picture 4.</label>
    <input type="file" id="file3" name="files[]" multiple="multiple" accept="image/*" />
</div>

这里是php代码: 注意:我没有将任何变量中的图像文件名添加到查询中,一切正常,只需要处理图像上传并将路径存储到数据库中

<?php

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$pname= stripslashes($_REQUEST['name1']);
$pcat= stripslashes($_REQUEST['name2']);
$pprice= stripslashes($_REQUEST['name3']);
$pqty= stripslashes($_REQUEST['qty']);
$pdesc=stripslashes($_REQUEST['description']);
//$spassword=stripslashes($_REQUEST['img']);



$sqlinsert ="INSERT INTO `noorizone`.`products` VALUES ('$pname', '$pcat', '$pprice')";

    if($con-> query($sqlinsert)=== true)
{
    echo "<center><b style='color:green;'> added  successfully... </b>";
    echo"</center>";
}
else{
    echo "<b style='color:red;'> cant register". $con->error."</b>";
}
}
?>

感谢提前!!!

3 个答案:

答案 0 :(得分:0)

这是我的例子,你可以学习我的代码:

$fileImageStatus = "";                      // Create your variable for files
if(isset($_FILES['files_status']['name'])){ // Checking value for all input
  $fileImageName = array();                 // Make an array
  for($i = 0; $i < count($_FILES['files_status']['name']); $i++){ // Looping
      $imageName = $_FILES['files_status']['name'][$i];         // Give a variable with index from array looped
      $uploadPath = '././sistem/users/members/'.$idPenerima.'/unggahan/';
      move_uploaded_file($imageTmp, $uploadPath.$imageName);    // Path upload
      $fileImageName[] = $imageName;                           // Setting a values from array
  }
      $fileImageStatus = implode(",", $fileImageName);         // Make that values an string
   }
  // in here you can insert to database with value from $fileImageStatus

答案 1 :(得分:0)

我认为如果您将文件名更改为file1而不是file [],那么您将没事。

查看W3Schools.com

中的示例

<!DOCTYPE html>
<html>
<body>
<!--
<?php


$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}




?>
-->
<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

答案 2 :(得分:-1)

这是一个关于如何将多个图像上传到PHP和Mysql的好教程

http://www.wdb24.com/how-to-upload-multiple-images-in-php-and-store-in-mysql/