上传两个且只有两个文件并保存在php中的不同文件夹中

时间:2014-12-22 19:56:45

标签: php codeigniter upload

我有一个小的运行代码,但缺少点点滴滴。我希望能够上传两个文件(不能少,不多),并将它们保存在不同的文件夹中。文件是“exe”。

文件:fileA,fileB

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

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

问题是,我有表单,允许多次上传,但不加载第二个文件。我所看到的,是第一个上传并保存在/home/abc/Downloads/createA/fileA

中的文件

代码:

  <?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>

1 个答案:

答案 0 :(得分:1)

本声明:

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

和本声明:

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

应该进行单独评估,但目前你将第二个嵌套到第一个