我遇到了一個我一直在努力的代碼問題。我只是無法弄清楚是什麼原因導致了這個錯誤。array_keys()期望參數1是數組,空給出
所以,我在CMS做一個上傳表單。如果我上傳9張照片,效果很好。如果我上傳超過9個文件,它給了我這個錯誤:
array_keys() expects parameter 1 to be array, null given in C:...upload.php on line 18
有人可以幫我嗎?
代碼:
$file_ary = reArrayFiles($_FILES['ufile']);
//Reorganising the $_FILES:
function reArrayFiles(&$file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
foreach ($file_ary as $file) {
$dir = "imgs/" . $last_fotoid . "/";
if (!file_exists($dir)) {
mkdir("imgs/" . $last_fotoid . "/");
}
$target_file = $dir . basename($file["name"]);
move_uploaded_file($file["tmp_name"], $target_file);
}
我的形式:
<form method="post" action="upload.php" enctype="multipart/form-data">
Select image to upload:<br>
<input type="file" name="ufile[]" multiple>
<input type="submit" value="Upload Image" name="submit">
</form>
您複製的代碼沒有多大幫助。但基本上你需要調用** ** reArrayFiles方法時,要確保,你傳遞一個數組 – Ali
$ _FILES [「ufile」]是一個數組,所以它應該工作 – laurensvanheerde
好,並非總是如此,只有當你提交表單。看看你發佈的代碼是不可能的。 – Ali