如何使用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为每个用户以不同的名称上传相同的文件。的主要内容,如果未能解决你的问题,请参考以下文章

如何在淘汰赛 foreach 绑定中使用表单

使用指针将数组中的两个对象(每个对象与一个类)相关联

如何分离查询结果以与其他查询结果对应

如何从用户定义的输入中上传图像以做出反应?

如何检查未知的 XML 文件以获取其元素或属性?

如何在通过php存储在文件夹中时更改图像名称