我有表格,我有三个文件输入。使用下面的代码,我尝试使用一些随机哈希和文件名将文件从TMP本地化移动到./uploads。
我遇到foreach
的问题,因为它在第一次迭代并将文件保存到上传目录后停止了。我不知道为什么表格中的其他两个元素没有发生。整个功能如下,有点乱,但我希望这是可以理解的。
function saveFile(){
global $patchFile;
$fileArray = [ $_FILES['file_one']['name'], $_FILES['file_two']['name'], $_FILES['file_three']['name'] ];
$tmpArray = [ $_FILES['file_one']['tmp_name'], $_FILES['file_two']['tmp_name'], $_FILES['file_three']['tmp_name'] ];
$multiArray =
[
[$_FILES['file_one']['name'], $_FILES['file_one']['tmp_name']],
[$_FILES['file_two']['name'], $_FILES['file_two']['tmp_name']],
[$_FILES['file_three']['name'], $_FILES['file_three']['tmp_name']]
];
foreach ($multiArray as $key)
{
echo "<br />Key: ".$key[0]."\n";
echo "Key_tmp: ".$key[1]."\n";
$randomString = generateRandomString();
$patchFile = './uploads/'.$randomString.$key[0];
echo "<br />Check patchFile: $patchFile";
if(is_uploaded_file($key[1]))
{
echo "<br />Begin uploading to directory...<br />";
if(!move_uploaded_file($key[1], $patchFile))
{
echo 'Problem: Nie udało się skopiować pliku do katalogu.';
return false;
}
else {
echo "File was saved in uploads directory";
return true;
}
}
else
{
echo "Uploading to directory... FAILED!";
}
}
}
答案 0 :(得分:2)