简体   繁体   English

如何将文件上传到两个不同的文件夹

[英]How to upload files to two different folder

I'm having trouble to upload 2 files to 2 different folders.I only able to upload one file to one selected folders.我无法将 2 个文件上传到 2 个不同的文件夹。我只能将一个文件上传到一个选定的文件夹。

Here's the screenshot of it:这是它的屏幕截图:

我设法上传了两个文件,但它在同一个文件夹中。

Here's the code:这是代码:

<form name="form_id" id="form_id" action="javascript:void(0);" enctype="multipart/form-data" style="width:800px; margin-top:20px;">  

    <div class="row">
        <div class="col-lg-12">
            <label class="fieldlabels">Documentation:</label> <br>
            <input type="file" name="vasplus_multiple_files" id="vasplus_multiple_files"  accept="application/pdf" multiple="multiple"/><br>  
            <!--input type="submit" value="Upload" /--><br>
            <table class="table table-striped table-bordered" style="width:60%;" id="add_files">
               <thead>
                  <tr>
                    <th style="color:blue; text-align:center;">File Name</th>
                    <th style="color:blue; text-align:center;">Status</th>
                    <th style="color:blue; text-align:center;">File Size</th>
                    <th style="color:blue; text-align:center;">Action</th>
                  <tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>                     

    </div> 
                            
    <input type="submit" value="Upload" /><br> 
</form>

<br/>

<form name="form_id2" id="form_id2" action="javascript:void(0);" enctype="multipart/form-data" style="width:800px; margin-top:20px;">  
   <div class="row">
        <div class="col-lg-12">
            <label class="fieldlabels">Photo:</label> <br>
            <input type="file" name="vasplus_multiple_files2" id="vasplus_multiple_files2" multiple="multiple" accept="image/*"/><br><br>        

            <table class="table table-striped table-bordered" style="width:60%;" id="add_files2">
                <thead>
                    <tr>
                      <th style="color:blue; text-align:center;">Photo Name</th>
                      <th style="color:blue; text-align:center;">Status</th>
                      <th style="color:blue; text-align:center;">File Size</th>
                      <th style="color:blue; text-align:center;">Action</th>
                    <tr>
                </thead>
                <tbody>

                </tbody>
                </table>
            </div>


        </div> 
                            
                        
       <input type="submit" value="Upload" /><br> 

</form>

And here's the upload.php code,in the code I just upload to one folder location:这是upload.php代码,在代码中我只是上传到一个文件夹位置:

<?php

if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST")
{
    $vpb_file_name = strip_tags($_FILES['upload_file']['name']); //File Name
    $vpb_file_id = strip_tags($_POST['upload_file_ids']); // File id is gotten from the file name
    $vpb_file_size = $_FILES['upload_file']['size']; // File Size
    $vpb_uploaded_files_location = 'C:/xampp/htdocs/claim_210119/claim_file/document/'; //This is the directory where uploaded files are saved on your server
    $vpb_final_location = $vpb_uploaded_files_location . $vpb_file_name; //Directory to save file plus the file to be saved
    //Without Validation and does not save filenames in the database
    if(move_uploaded_file(strip_tags($_FILES['upload_file']['tmp_name']), $vpb_final_location))
    {
        //Display the file id
        echo $vpb_file_id;
    }
    else
    {
        //Display general system error
        echo 'general_system_error';
    }

}
?>

How can I make the uploads in the two different forms to two different folders in my pc.Can anyone help on this?Thanks in advance.如何将两个不同的 forms 上传到我电脑中的两个不同文件夹。有人可以帮忙吗?提前致谢。

Do you want to save one file in location A and another in location B?是否要将一个文件保存在位置 A 而另一个文件保存在位置 B?

You can simply use the submit button's name attribute to identify which form's values come to PHP file and then use the desired location.您可以简单地使用提交按钮的name属性来识别PHP文件中的表单值,然后使用所需的位置。

So add another expression inside if condition like:因此,在 if 条件中添加另一个表达式,例如:

if (isset($_POST["form1"])) // name of first form's submit button
{
  // Your desired location
}
else
{
  // Your desired location
}

// Rest of your logic is applicable for any location :)

Add name attribute in submit buttons in both forms like:在 forms 的提交按钮中添加name属性,例如:

<input type="submit" name="form1" value="Upload" />

and

<input type="submit" name="form2" value="Upload" />
// Try following code
if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST")
{
if(!empty($_FILES['vasplus_multiple_files'])){
    //for first form.
    try{
           $vpb_file_name = strip_tags($_FILES['vasplus_multiple_files']['name']); //File Name   
           $vpb_uploaded_files_location = './claim_file/document/'; //This is the directory where uploaded files are saved on your server
           $vpb_final_location = $vpb_uploaded_files_location . $vpb_file_name; //Directory to save file plus the file to be saved
         
           $tmp_file_path = strip_tags($_FILES['vasplus_multiple_files']['tmp_name']);
           //Without Validation and does not save filenames in the database
   
           move_uploaded_file($tmp_file_path, $vpb_final_location);
           echo $vpb_file_name; //Display the file name
enter code here
    }catch(Exception $ex){
           echo $ex->getMessage();  //Display general system error
    } 
}
    //for second form.
      if(!empty($_FILES['vasplus_multiple_files2'])){
    try{ 

           $vpb_file_name_new = strip_tags($_FILES['vasplus_multiple_files2']['name']); //File Name   
           $vpb_uploaded_files_location_new = './claim_file/documentNew/'; //This is the directory where uploaded files are saved on your server
           $vpb_final_location_new = $vpb_uploaded_files_location_new . $vpb_file_name_new; //Directory to save file plus the file to be saved
         
           $tmp_file_path_new = strip_tags($_FILES['vasplus_multiple_files2']['tmp_name']);
           //Without Validation and does not save filenames in the database
   
           move_uploaded_file($tmp_file_path_new, $vpb_final_location_new);
           echo $vpb_file_name_new; //Display the file name
    }catch(Exception $ex){
           echo $ex->getMessage();  //Display general system error
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM