2017-06-15 48 views
0

我想添加多个图片到网页,但它只允许我添加1.我试图添加超过3个图像到网页上,所有ive完成是添加一个按钮,可以让你选择文件,但我想更多的1档和4张图片最低要上传 继承人的代码:试图将多个图片添加到网页上php

<section class="left"> 
    <ul> 
     <li><a href="manufacturers.php">Manufacturers</a></li> 
     <li><a href="bikes.php">Bikes</a></li> 

    </ul> 
</section> 

<section class="right"> 


<?php 


if (isset($_POST['submit'])) { 

    $stmt = $pdo->prepare('INSERT INTO bikes (model, description, price, manufacturerId) 
          VALUES (:model, :description, :price, :manufacturerId)'); 

    $criteria = [ 
     'model' => $_POST['model'], 
     'description' => $_POST['description'], 
     'price' => $_POST['price'], 
     'manufacturerId' => $_POST['manufacturerId'] 
    ]; 

    $stmt->execute($criteria); 

    if ($_FILES['image']['error'] == 0) { 
     $fileName = $pdo->lastInsertId() . '.jpg'; 
     move_uploaded_file($_FILES['image']['tmp_name'], '../images/bikes/' . $fileName); 
    } 

    echo 'Bike added'; 
} 
else { 
    if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) { 
    ?> 


     <h2>Add Product</h2> 

     <form action="addbike.php" method="POST" enctype="multipart/form-data"> 
      <label>Bike Model</label> 
      <input type="text" name="model" /> 

      <label>Description</label> 
      <textarea name="description"></textarea> 

      <label>Condition</label> 
      <input type="text" name="Condition" /> 

      <label>Price</label> 
      <input type="text" name="price" /> 

      <label>Category</label> 

      <select name="manufacturerId"> 
      <?php 
       $stmt = $pdo->prepare('SELECT * FROM manufacturers'); 
       $stmt->execute(); 

       foreach ($stmt as $row) { 
        echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>'; 
       } 

      ?> 

      </select> 

      <label>Bike image</label> 

      <input type="file" name="image" /> 

      <input type="submit" name="submit" value="Add Product" /> 

     </form> 



    <?php 
    } 

    else { 
     ?> 
     <h2>Log in</h2> 

     <form action="index.php" method="post"> 
      <label>Username</label> 
      <input type="text" name="username" /> 

      <label>Password</label> 
      <input type="password" name="password" /> 

      <input type="submit" name="submit" value="Log In" /> 
     </form> 
    <?php 
    } 

} 
?> 

+0

图像名称应该是一个数组也可以使用jquery –

+0

你必须使用文件名阵列和添加'multiple'属性到文件输入标签 'input type =“file”name =“image []”multiple />' –

+0

added 但是当我离开管理员页面图像不可见 – Pactz

回答

1

要允许从输入文件multiple文件选择会像

<input type="file" name="image" multiple> 

要提取选择

print_r($_FILES); // will return you detail of all files in array 

输入标签的多个属性中的所有文件并不在互联网 支持Explorer 9和更早版本。

0

您可以一次选择多个文件,但您需要添加多个文件,然后您可以选择这样的文件。

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
<body> 
    <form action="upload.php" method="post" multipart="" enctype="multipart/form-data"> 
     <input type="file" name="img[]" multiple> 
     <input type="submit"> 
    </form> 
</body> 
</html> 

<?php 
echo '<pre>'; 
$img = $_FILES['img']; 

if(!empty($img)) 
{ 
    $img_desc = reArrayFiles($img); 
    print_r($img_desc); 

    foreach($img_desc as $val) 
    { 
     $newname = date('YmdHis',time()).mt_rand().'.jpg'; 
     move_uploaded_file($val['tmp_name'],'./uploads/'.$newname); 
    } 
} 

function reArrayFiles($file) 
{ 
    $file_ary = array(); 
    $file_count = count($file['name']); 
    $file_key = array_keys($file); 

    for($i=0;$i<$file_count;$i++) 
    { 
     foreach($file_key as $val) 
     { 
      $file_ary[$i][$val] = $file[$val][$i]; 
     } 
    } 
    return $file_ary; 
} 
+0

添加但是当我离开管理页面时图像不可见 – Pactz