Copy files to another folder and rename - PHP

Asked

Viewed 325 times

-1

How to rename a file after copying and pasting from one folder to another. It even renames, but only if I refresh the page, IE, I’m not able to process both functions at the same time.

Follow the code below:

<?php 

include 'var.php';


// ### =============================== ###
// ###  Converter nome dos arquivos    ###
// ### =============================== ###


if($_mes <= '09'){ $_mes = substr($_data,6,1); } else { $_mes = substr($_data,5,2); }


if(isset($_data)){ 

    if(is_dir($_In)){ /*echo $_In;*/

    if(is_dir($mes_Out)){

        $_g = glob("{$mes_Out}\*.pdf");
        // var_dump($_g);

        if(empty($_g)){ exec("xcopy ".$mes_In." ".$mes_Out);
        } else { echo "Já existem arquivos nesse diretório.";}

        foreach($_g as $files){             

            $fileName = substr($files, 43);
            $n = substr($fileName, 0, 16).".pdf";

            $_date = substr($n, 6, 16);
            $_dt = str_replace('_','',$_date);
            $_y = substr($_dt, 4, 4);
            $_m = substr($_dt, 2, 2);
            $_d = substr($_dt, 0, 2);
            $_Mdt = $_y.$_m.$_d; // Monta Data

            $dcm_hash = md5($n);
            $length_Hash = substr($dcm_hash,0,8);
            $_Fname = $_Mdt.$length_Hash.".pdf";


            rename($files, $mes_Out.$_DS.$_Fname); // ###  Renomeia arqs. do diretório de saída
            set_time_limit(100000); 
            echo "Arquivos renomeados!"."<br>";

        }           
    }
// ----------------------------------------------------------------------------------

    }
}
?>

1 answer

2

Solution: Include a new check in the directory, from the current state (after copying the files)

   <?php 

    include 'var.php';

    if($_mes <= '09'){ $_mes = substr($_data,6,1); } else { $_mes = substr($_data,5,2); }


    if(isset($_data)){ 

        if(is_dir($_In)){ 

        if(is_dir($mes_Out)){

            // ###  ================ ###
            // ###  Copiar arquivos  ###
            // ###  ================ ###

            $_arqs1 = glob("{$mes_Out}\*.pdf"); 

            echo(!empty(**$_arqs1**) ? "Já existem arquivos nesse diretório."."<br>" : exec("xcopy ".$mes_In." ".$mes_Out)."<br>"."Arquivos copiados com sucesso!"."<br>");


            // ###  =================  ###
            // ###  Renomear arquivos  ###
            // ###  =================  ###


            $_arqs2 = glob("{$mes_Out}\*.pdf"); // Linha incluída


            foreach($_arqs2 as $files){             

                $fileName = substr($files, 43);
                $n = substr($fileName, 0, 16).".pdf";

                $_date = substr($n, 6, 16);
                $_dt = str_replace('_','',$_date);
                $_y = substr($_dt, 4, 4);
                $_m = substr($_dt, 2, 2);
                $_d = substr($_dt, 0, 2);
                $_Mdt = $_y.$_m.$_d; // Monta Data

                $dcm_hash = md5($n);
                $length_Hash = substr($dcm_hash,0,8);
                $_Fname = $_Mdt.$length_Hash.".pdf";


                rename($files, $mes_Out.$_DS.$_Fname); // ###  Renomeia arqs. do diretório de saída
                set_time_limit(100000); 
                echo "Arquivos renomeados!"."<br>";

            }           
        }
    // ----------------------------------------------------------------------------------
  }
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.