Tilføj flere filinput i din formular. En måde er at bruge et matrixnavn - 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 />
Derefter i din uploader.php
, indpak din filoverførselskode med en for-løkke
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 et godt afsnit om almindelige faldgruber ved upload af filer, især flere. http://www.php.net/manual /da/features.file-upload.common-pitfalls.php
Hvis du vil gøre flere af de andre, kan det gøres på samme måde (jeg forkortede valgene for at reducere kopier/indsæt) -
<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>
og din php, sæt for-løkken rundt om alle elementerne
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
rediger
du er der næsten. Fjern dubletkoden over for-løkken. Fjern basename()
, da dette forårsager din extension
at mislykkes, og pathinfo()
returnerer ['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
?>