私が推測するように、主な問題は HTML 要素名と で使用されている名前$_FILES
が一致しないことです。つまり、最初のファイル入力名「file」を使用しました。「picture_1」である必要があります。そして、ファイル処理部分でインデックス 0 から 2 を使用しました。「picture_1」、「picture_2」、および「picture_3」に一致するように、1 ~ 3 にする必要があります。
フォームに が含まれている必要があることに注意してくださいenctype="multipart/form-data"
。そうでない場合、ファイルはアップロードされません。これが正しいものです:
これを実装するには、次の 2 つの方法が
あり
ます。
file[]
アプローチ 1: ファイル入力に個別に名前を付ける
HTML
<form method="post" enctype="multipart/form-data">
<li>
<label for="picture_1">picture 1 : </label>
<input type="file" name="picture_1" id="picture_1" />
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="picture_2" id="picture_2" />
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="picture_3" id="picture_3" />
</li>
<input type="submit" name="submit" value="Upload" />
</form>
PHP
if(sizeof($_FILES)){
for($i = 1; $i <= 3; $i++) {
$aFile = $_FILES['picture_'.$i];
if(empty($aFile['tmp_name'])) continue; # skip for empty elements
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $aFile["name"]));
if ((($aFile["type"] == "image/gif")
|| ($aFile["type"] == "image/jpeg")
|| ($aFile["type"] == "image/png")
|| ($aFile["type"] == "image/pjpeg"))
&& ($aFile["size"] < 20000)
&& in_array(strtolower($extension), $allowedExts))
{
if ($aFile["error"] > 0)
{
echo "Return Code: " .$aFile["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $aFile["name"]))
{
echo $aFile["name"] . " already exists. ";
}
else
{
move_uploaded_file($aFile['tmp_name'],
"upload/" . date('U')."-".$aFile["name"]);
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
}
アプローチ 2: ファイル入力をグループとして命名する
HTML
<form method="post" enctype="multipart/form-data">
<li>
<label for="picture_1">picture 1 : </label>
<input type="file" name="file[]" id="picture_1" />
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="file[]" id="picture_2" />
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="file[]" id="picture_3" />
</li>
<input type="submit" name="submit" value="Upload" />
</form>
PHP
if(sizeof($_FILES)){
for($i = 0; $i < 3; $i++) {
$name = $_FILES['file']['name'][$i];
$type = $_FILES['file']['type'][$i];
$tmp_name = $_FILES['file']['tmp_name'][$i];
$error = $_FILES['file']['error'][$i];
$size = $_FILES['file']['size'][$i];
if(empty($name)) continue; # skip for empty element
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $name));
if (( ($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/png")
|| ($type == "image/pjpeg"))
&& $size < 20000
&& in_array(strtolower($extension), $allowedExts) )
{
if ($error > 0)
{
echo "Return Code: " .$error . "<br>";
}
else
{
if (file_exists("upload/" . $name))
{
echo $aFile["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($tmp_name,
"upload/" . date('U')."-".$name);
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
}
クレジット:
- ファイル拡張子は、
strtolower()を使用して小文字でチェックする必要があります。
- を使用する場合
<label for="some_id">
、それぞれの HTML 要素に同じ ID 属性を含めることができます (例: <input
type="file" name="..." id="some_id" />
. ラベルをクリックすると、要素のonclickイベントがトリガーされます。