Lägg till flera filinmatningar i ditt formulär. Ett sätt är att använda ett arraynamn - image[]
Image to upload: <input type="file" name="image[]" /><br />
Image to upload: <input type="file" name="image[]" /><br />
Image to upload: <input type="file" name="image[]" /><br />
.... // as many as you want. Just be aware of upload_max_filesize, memory_limit, post_max_size etc.
<br />
Sedan i din uploader.php
, slå in din filuppladdningskod med en for-loop
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));
...
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path))
{
...
echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />";
}
else
{
echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
} // close your foreach
manualen har ett bra avsnitt om vanliga fallgropar vid uppladdning av filer, särskilt flera. http://www.php.net/manual /en/features.file-upload.common-pitfalls.php
Om du vill göra flera av de andra kan det göras på samma sätt (jag förkortade valen för att minska kopiera/klistra in) -
<form enctype="multipart/form-data" action="uploader.php" method="POST">
// 1st set
Category: <select class="text" name="dataType[]" />
...
</select><br />
<br />
Caption: <input type="text" name="title[]" /><br />
<br />
Image to upload: <input type="file" name="image[]" /><br />
<br />
// 2nd set
Category: <select class="text" name="dataType[]" />
...
</select><br />
<br />
Caption: <input type="text" name="title[]" /><br />
<br />
Image to upload: <input type="file" name="image[]" /><br />
<br />
// and so on, as many as you want
...
<input type="submit" value="Upload">
</form>
och din php, lägg for-slingan runt alla element
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
$dataType = mysql_real_escape_string($_POST["dataType"][$i]); // get the dataType with the same key - $i
$title = mysql_real_escape_string($_POST["title"][$i]); // get the title with the same key - $i
$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));
...
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path))
{
...
echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />";
}
else
{
echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
} // close your foreach
redigera
du är nästan där. Ta bort dubblettkoden ovanför for-slingan. Ta bort basename()
, eftersom detta orsakar ditt extension
att misslyckas och pathinfo()
returnerar ['basename']
.
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
if($_FILES["image"]["name"][$i] != ''){ // don't insert if file name empty
$dataType = mysql_real_escape_string($_POST["dataType"][$i]);
$title = mysql_real_escape_string($_POST["title"][$i]);
$fileData = pathinfo($_FILES["image"]["name"][$i]);
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName;
while(file_exists($target_path)){
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName;
}
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path)){ // The file is in the images/gallery folder.
// Insert record into database by executing the following query:
$sql="INSERT INTO images (data_type, title, file_name) "."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);
echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />
<a href='index.php'>Add another image</a><br />";
}
else
{
echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
}
} // close your foreach
?>