刪除空上傳我已經做了函數的單層文件上傳和多文件上傳到影響工作,當檢測到多文件上傳不使用PHP的默認陣列設置和更易於管理的陣列被製成。PHP多文件上傳,從陣列
多個文件HTML:
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="file" name="userFile[]" class="userFile">
<input type="file" name="userFile[]" class="userFile">
<input type="file" name="userFile[]" class="userFile">
<input type="submit" name="submit" value="Upload">
</form>
替代單個文件HTML:
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="file" name="userFile" class="userFile">
<input type="submit" name="submit" value="Upload">
</form>
PHP:
function checkUploadedFile()
{
$file;
$files;
$file_arr_keys;
foreach($_FILES as $element) {
if (is_array($element['name'])) {
$file_count = count($element['name']);
$file_arr_keys = array_keys($element);
for ($i = 0; $i < $file_count; $i++) {
foreach ($file_arr_keys as $key) {
$files[$i][$key] = $element[$key][$i];
}
}
} else {
$file = $element;
}
}
if (isset($file)) return $file;
return $files;
}
會造成這樣的事情,如果多個文件的形式被使用,並且只一個文件被上傳:
Array
(
[0] => Array
(
[name] => IMG_0015.JPG
[type] => image/jpeg
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpMEaSSc
[error] => 0
[size] => 5191142
)
[1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
問題:如何更改上面的輸出陣列,使得其僅具有一個元素,如果其他文件中的元素是空?在:
Array
(
[0] => Array
(
[name] => IMG_0015.JPG
[type] => image/jpeg
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpMEaSSc
[error] => 0
[size] => 5191142
)
)
檢查'error'字段。 –