我正在尝试上传多个图像并将每个路径存储在一个id中,但是在php mysql中的一个单独的列中

时间:2018-06-02 12:48:48

标签: php mysql

这是我的HTML代码

<form action="" method="post" enctype="multipart/form-data">
              <div class="form-row">
                <div class="form-group col-md-6">
                  <label for="inputEmail4">Product Name</label> (<small class="text-muted">required</small>)
                  <input type="text" class="form-control" id="inputEmail4" name="prdct_name" placeholder="Product name" required="required">
                  <small id="passwordHelpBlock" class="form-text text-muted">
                     please don't input special characters, like numbers, %, $ @ and so on. 
                  </small>
                </div>
                <div class="form-group col-md-6">
                  <label for="inputPassword4">Category</label> (<small class="text-muted">required</small>)
                  <input type="text" class="form-control" id="inputPassword4" placeholder="category" required="required" name="cate_gory">
                  <small id="passwordHelpBlock" class="form-text text-muted">
                    write the category of the product. eg: Electronics, Men shoes, etc.
                  </small>
                </div>
                <div class="form-group col-md-6">
                 <label for="inputPassword4">product image</label> (<small class="text-muted">required</small>)
                  <div class="custom-file">
                  <input type="file"  name="files[]" class="custom-file-input" id="customFile" required="required" multiple>
                  <label class="custom-file-label" for="customFile">Choose files</label>
                </div>
                  <small id="passwordHelpBlock" class="form-text text-muted">
                    You are allow to select  one to four images with png or jpg extension, beyond that will not be accepted.
                  </small>
                </div>

              </div>
              <div class="form-group">
                <label for="exampleFormControlTextarea1">Product description</label> (<small class="text-muted">required</small>)
                <textarea class="form-control" id="exampleFormControlTextarea1" rows="3" required="required" name="about_us" placeholder="product description"></textarea>
                <small id="passwordHelpBlock" class="form-text text-muted">
                    Write the description of the current product you are uploading.
                  </small>
              </div>
              <button type="submit" class="btn btn-success" name="upl_prdt">upload</button>
           </form>

Php代码

 if (isset($_POST['upl_prdt']) && isset($_POST['prdct_name']) && isset($_POST['cate_gory'])  && isset($_FILES['files'])) {
  $product_name        =  htmlentities(strip_tags(mysqli_escape_string($con,$_POST['prdct_name'])));
  $category            =  htmlentities(strip_tags(mysqli_escape_string($con,$_POST['cate_gory'])));
  $desc                =  htmlentities(strip_tags(mysqli_escape_string($con,$_POST['about_us'])));
  $images              =  $_FILES['files'];
  $allowed_files       =  array('jpg', 'jpeg', 'gif', 'png');
  $file_img = array();



    if (strpbrk($product_name, '0123456789')) {
        $error[] =  "please your product name must not contain numbers.";
    }
    if (strlen($product_name) <= 2) {
        $error[] = "the length of your product name must  be at least 3 characters.";
    }
    if (strlen($product_name) >= 32) {
        $error[] = "the length of your product name is too long.";
    }
    if (strpbrk($category, '0123456789')) {
        $error[] =  "please your category must not contain numbers.";
    }
    if (strlen($category) <= 2) {
        $error[] = "the length of your category must  be at least 3 characters.";
    }
    if (strlen($category) >= 32) {
        $error[] = "the length of your category is too long.";
    }
    if (count($_FILES['files']['name']) > 4) {
        $error[] = "Sorry you are only limited to four pictures for one product.";
     }


  foreach ($images['name'] as $position => $img_name) {
                $file_n      = $images['name'][$position];
                $file_tmp    = $images['tmp_name'][$position];
                $file_size   = $images['size'][$position];
                $file_error  = $images['error'][$position];


               $file_ext    = explode('.', $img_name);
               $file_ext    =  strtolower(end($file_ext));
    if (in_array($file_ext, $allowed_files) != true) {
        $error[] = "This file extension".' <strong>'. $file_ext .'</strong> '."is not allowed";

    }
    if ($file_size > 3097152) {
        $error[] = "This ".' <strong>'. $img_name .'</strong> '."size is too big";
    }

  }

enter code here


      $random_name = rand();
        if (empty($_POST) === false && empty($error) === true) {
                $name_array = $_FILES['files']['name'];
                $tmp_name_array = $_FILES['files']['tmp_name'];
                $type_array = $_FILES['files']['type'];
                $size_array = $_FILES['files']['size'];
                $error_array = $_FILES['files']['error'];
                for($i = 0; $i < count($tmp_name_array); $i++){
                    if($file_destination[] = move_uploaded_file($tmp_name_array[$i], "uploads/".$random_name.$name_array[$i])){
                        foreach ($file_destination as $key => $value) {
                            var_dump($value);
                        }
                    } else {
                        echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
                    }
                }

        }

1 个答案:

答案 0 :(得分:0)

查看页面

class Program
{
static void Main()
{
    SiteStructure s = SiteStructure.Instance;
}
}

public sealed class SiteStructure
{
static readonly SiteStructure _instance = new SiteStructure();
public static SiteStructure Instance
{
    get
    {
        return _instance;
    }
}
SiteStructure()
{
    // Initialize.
}
}

            注意:支持的图像格式:.jpeg,.jpg,.png,.gif                                                          

upload.php的

<form action="upload.php" method="post" enctype="multipart/form-data"><table width="100%"><tr><td>Select Photo (one or multiple):</td><td><input type="file" name="images[]" multiple/></td></tr>