如何使用Foreach为每个用户以不同的名称上传相同的文件。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Foreach为每个用户以不同的名称上传相同的文件。相关的知识,希望对你有一定的参考价值。
Foreach($_POST['...]...).其他查询通过foreach循环工作,但文件上传为数组的第1个索引,这不是多文件上传,我想为每个用户以不同的名称上传相同的文件。
foreach($_POST['groupmem'] as $user)
//Some Queries
$filename2 = str_replace(" ", "_","$user.$_FILES['proposal']['name']");
$destination2 = '../img/proposal/' . $filename2;
$extension2 = pathinfo($filename2, PATHINFO_EXTENSION);
$file2 = $_FILES['proposal']['tmp_name'];
$size2 = $_FILES['proposal']['size'];
if (!in_array($extension2, ['zip', 'pdf', 'docx']))
echo "You file extension must be .zip, .pdf or .docx";
elseif ($_FILES['proposal']['size'] > 200000000) // file shouldn't be larger than 200Megabyte
echo "File too large!";
else
if (move_uploaded_file($file2, $destination2))
$sql = "UPDATE project SET proposal_name='$filename2' WHERE u_id='$user' ";
if (mysqli_query($conn, $sql))
echo "File uploaded successfully";
else
echo "Failed to upload file.";
答案
你不能在循环中进行move_uploaded_file的操作。
$user1 = $_POST['groupmem'][0];
$filename1 = str_replace(" ", "_","$user1.$_FILES['proposal']['name']");
$destination1 = '../img/proposal/' . $filename1;
$extension1 = pathinfo($filename1, PATHINFO_EXTENSION);
$file1 = $_FILES['proposal']['tmp_name'];
$size1 = $_FILES['proposal']['size'];
if (!in_array($extension1, ['zip', 'pdf', 'docx']))
echo "You file extension must be .zip, .pdf or .docx";
elseif ($_FILES['proposal']['size'] > 200000000) // file shouldn't be larger than 200Megabyte
echo "File too large!";
else
if (move_uploaded_file($file1, $destination1))
foreach($_POST['groupmem'] as $user)
$filename2 = str_replace(" ", "_","$user.$_FILES['proposal']['name']");
$destination2 = '../img/proposal/' . $filename2;
if ($user <> $user1)
if (!copy($destination1, $destination2)) echo "failed to copy $file...\n";
$sql = "UPDATE project SET proposal_name='$filename2' WHERE u_id='$user' ";
if (mysqli_query($conn, $sql))
echo "File uploaded successfully";
else
echo "Failed to upload file.";
以上是关于如何使用Foreach为每个用户以不同的名称上传相同的文件。的主要内容,如果未能解决你的问题,请参考以下文章