我猜,主要問題是您的HTML元素名稱和$_FILES
中使用的名稱無法匹配,即您使用的是第一個文件輸入名稱「file」。它應該是「picture_1」。你在你的文件處理部分使用索引0到2。匹配「picture_1」,「picture_2」和「picture_3」應該是1到3。
請注意,您的表單應該有enctype="multipart/form-data"
,否則您的文件將不會上傳。這裏是正確的:
存在實現這兩種方法:
(1)命名的文件輸入分別如picture_1,picture_2等
(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";
}
}
}
APPROACH 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";
}
}
}
鳴謝:
- 文件擴展名必須用012較低的情況下進行檢查strtolower()。
- 如果您使用
<label for="some_id">
,您可以在您的相應HTML元素中擁有相同的ID屬性,例如<input type="file" name="..." id="some_id" />
。當您單擊標籤時,元素的事件將被觸發。
檢查'if'語句中的哪個布爾值是'false'?調試101. – Halcyon
你試過先上傳一個文件嗎? – Arunu
顯示您的HTML。您的html輸入文件名可能是錯誤的。 – Sithu