2016-03-12 179 views
2

我正在創建圖庫網站,並且希望僅使用PHP和MYSQLI創建一個多圖像上傳器...我不擅長編碼,因此本網站上的多圖像上傳的其他示例沒有不爲我工作。多圖像上傳

以下是根據當前用戶會話將數據發佈到數據庫中的工作代碼。

HTML:

<form action="upload.php" method="post" enctype="multipart/form-data"> 
<label for="image">Select a file:</label> 
<input type="file" name="image[]" id="image" multiple='multiple' /> 
<br /><br> 
<label for="picname">Title:</label> 
<input type="text" name="picname" id="picname" /> 
<br /><br> 
<label for="picdesc">Description:</label> 
<input type="text" name="picdesc" id="picdesc" /> 
<br /><br> 
<label for="piccat">Category:</label> 
<select name="piccat" id="piccat"> 
<option value="--"></option> 
<option value="animation">Animation</option> 
<option value="illustration">Illustration</option> 
<option value="photography">Photography</option> 
</select> 
<br /><br> 
<input type="submit" name="submit" value="Submit" /> 
</form> 

PHP &的mysqli:

<?php 
$path = "images/projects/"; 
include("check.php"); 

if (isset($_POST["submit"])) { 
     $image = $_FILES["image"]["tmp_name"]; 
     $imageName = $_FILES["image"]["name"]; 
     $imageSize = $_FILES["image"]["size"]; 
     $imageType = $_FILES["image"]["type"]; 
     $imageTitle = $_POST["picname"]; 
     $imageDescription = $_POST["picdesc"]; 
     $imageCategory = $_POST["piccat"]; 
     $len = count($image); 
     $path = $path . $imageName; 

     $query = $db -> prepare("INSERT INTO images (user_id, image, description, type, title, size, category) 
     VALUES (?, ?, ?, ?, ?, ?, ?)"); 
     $query -> bind_param('issssis', $_SESSION['user_id'], $imageName, $imageDescription, $imageType, $imageTitle, $imageSize, $imageCategory); 
     $query -> execute(); 
     $query -> close();   

    if ($imageName){ 
      move_uploaded_file($image, $path); 
      } 
} 
?> 

它工作正常,但只能上傳一個圖片。如何選擇多個圖像並一次上傳所有圖像?

+0

有一個真棒爲你正在嘗試做的。 https://github.com/samayo/bulletproof – Ikari

+0

哦,謝謝你,但我在大學的課程這樣做,不幸的是,我們不允許使用任何第三方或類似的東西... – Elina

+0

http:// stackoverflow .com/questions/24895170/multiple-image-upload-php-form-with-one-input,http://php.net/manual/en/features.file-upload.multiple.php –

回答

4

使用這一個

<?php 
$path = "images/projects/"; 
include("check.php"); 
if (isset($_POST["submit"])) { 
    for ($i = 0; $i < count($_FILES["image"]["name"]); $i++) { 
    $image = $_FILES["image"]["tmp_name"][$i]; 
    $imageName = $_FILES["image"]["name"][$i]; 
    $imageSize = $_FILES["image"]["size"][$i]; 
    $imageType = $_FILES["image"]["type"][$i]; 
    $imageTitle = $_POST["picname"]; 
    $imageDescription = $_POST["picdesc"]; 
    $imageCategory = $_POST["piccat"]; 
    $path = $path . $imageName; 

    $query = $db -> prepare("INSERT INTO images (user_id, image, description, type, title, size, category) 
    VALUES (?, ?, ?, ?, ?, ?, ?)"); 
    $query -> bind_param('issssis', $_SESSION['user_id'], $imageName, $imageDescription, $imageType, $imageTitle, $imageSize, $imageCategory); 
    $query -> execute(); 
    $query -> close();   

if ($imageName){ 
     move_uploaded_file($image, $path); 
     } 
} 
} 
?> 
+0

我測試了這段代碼,它在每個循環中產生圖像信息。你可以分享你的move_uploaded_file($ image,$ path)方法。 –

+0

一次從同一文件夾中選擇多個圖像,您將獲得圖像數量而不是名稱! –

1

使用這一個

<?php 
$path = "images/projects/"; 
include("check.php"); 
if (isset($_POST["submit"])) { 
    for ($i = 0; $i < count($imageName); $i++) { 
     $image = $_FILES["image"]["tmp_name"][$i]; 
     $imageName = $_FILES["image"]["name"][$i]; 
     $imageSize = $_FILES["image"]["size"][$i]; 
     $imageType = $_FILES["image"]["type"][$i]; 
     $imageTitle = $_POST["picname"]; 
     $imageDescription = $_POST["picdesc"]; 
     $imageCategory = $_POST["piccat"]; 
     $path = $path . $imageName; 

     $query = $db -> prepare("INSERT INTO images (user_id, image, description, type, title, size, category) 
     VALUES (?, ?, ?, ?, ?, ?, ?)"); 
     $query -> bind_param('issssis', $_SESSION['user_id'], $imageName, $imageDescription, $imageType, $imageTitle, $imageSize, $imageCategory); 
     $query -> execute(); 
     $query -> close();   

    if ($imageName){ 
      move_uploaded_file($image, $path); 
      } 
} 
} 
?> 
+0

我這樣做,但它仍然是相同...上傳了多張圖片,並沒有選擇,也是這個: 注意:數組字符串轉換在...上線17 注意:數組字符串轉換在...在線17 警告:move_uploaded_file()期望參數1是字符串,數組在第21行中給出 – Elina

+0

@Purushotam說:使用'$ _FILES [「image」] [「name」]'而不是'$ imageName'。這工作沒有顯示任何錯誤!你可以分享你的'move_uploaded_file()'功能代碼嗎? – Barett

+0

哦,是的,它現在工作沒有錯誤,但我仍然無法看到多個圖像上傳的選項...這裏它看起來像: https://drive.google.com/file/d/0B9x_thwSvLRvV2JMRnQtZjY4NFU/view ?usp = sharing 所以我只能選擇一個文件... – Elina