简体   繁体   English

仅上传两个文件,并保存在php中的不同文件夹中

[英]Upload two and only two files and save in different folder in php

I have a small running code but missing in bits and pieces. 我有一个小的运行代码,但一点一点地丢失了。 I want to be able to upload two files ( no less, no more) and save them in different folders. 我希望能够上传两个文件(不少于多),并将其保存在不同的文件夹中。 The files are "exe"s. 这些文件是“ exe”文件。

files: fileA,fileB 文件:fileA,fileB

File path for fileA: /home/abc/Downloads/createA/fileA fileA的文件路径: /home/abc/Downloads/createA/fileA

File path for fileB: /home/abc/Downloads/createB/fileB fileB的文件路径: /home/abc/Downloads/createB/fileB

The issue is, I have the form, which allows multiple upload, but doesn't load the second file. 问题是,我有一个表格,该表格允许多次上传,但不加载第二个文件。 All I see, is the first file getting uploaded and saved in /home/abc/Downloads/createA/fileA 我所看到的是第一个文件上传并保存在/home/abc/Downloads/createA/fileA

Code: 码:

  <?php

    if(isset($_FILES['files'])){

    $errors= array();

    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){

           //added from single upload

   if($file_size > 3000000){

        $errors[]='File size must be less than 3 MB';

    }       
   if(empty($errors)==true){
            echo  "Please make sure uploaded file name contains 'fileA' in the name";
            echo '<br/>';

   if(preg_match("/fileA/i", $file_name)){ 
           $upload_dir = "/home/abc/Downloads/createA/";
           $upload_dir_1 = "$upload_dir".$file_name;
           mkdir($upload_dir, 0777, true);
           move_uploaded_file($file_tmp,$upload_dir_1);

         // print_r($upload_dir);
         echo '<br/>';
         echo "Success ";
         echo "Stored in: " .$upload_dir;


   if (preg_match("/fileB/i", $file_name)){
        $upload_dir = "/home/abc/Downloads/createB/";
        $upload_dir_1 = "$upload_dir".$file_name;
        mkdir($upload_dir, 0777, true);
        move_uploaded_file($file_tmp, $upload_dir_1);
        echo '<br/>';
        echo "Loaded fileB";
        }

    else{
        echo "I am not uploading fileB";
    }
        }

    }

  else{
     echo "I am not loading fileA"; 
     print_r($errors);
     }
}


<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" /><br />
    <input type="file" name="files[]" /><br />
    <input type="submit"/>
</form>

This statement: 这个说法:

if(preg_match("/fileA/i", $file_name))

and this statement: 并且此语句:

if(preg_match("/fileB/i", $file_name))

should be evaluated separetly, but currently you have the second one nested into the first one 应该分开评估,但是目前您将第二个嵌套在第一个中

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

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