अपने फॉर्म में, कई फ़ाइल इनपुट जोड़ें। सरणी नाम का उपयोग करने का एक तरीका है - 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($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 /en/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($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
संपादित करें
आप लगभग वहां हैं। लूप के ऊपर के डुप्लीकेट कोड को हटा दें। 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
?>