フォームに、複数のファイル入力を追加します。 1つの方法は、配列名を使用することです-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 /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ループを置きます
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
?>