Във вашия формуляр добавете множество входни файлове. Един от начините е да използвате име на масив - 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 />
След това във вашия uploader.php
, обвийте кода си за качване на файла с цикъл for
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
ръководството има страхотен раздел за често срещаните клопки при качване на файлове, особено множество. http://www.php.net/manual /bg/features.file-upload.common-pitfalls.php
Ако искате да направите няколко от другите, това може да стане по същия начин (съкратих избраните, за да намаля копиране/поставяне) -
<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>
и вашия php, поставете цикъла for около всички елементи
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
редактиране
почти сте там. Премахнете дублиращия се код над цикъла for. Премахнете basename()
, тъй като това причинява вашето extension
да се провали и pathinfo()
ще върне ['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
?>