copy files to another folder with php

1

I have this function

 $srcfile='C:\xampp\htdocs\imagenes\*.*';
 $dstfile='C:\xampp\htdocs\imagenes-copiadas\*.*';
 mkdir(dirname($dstfile), 0777, true); copy($srcfile, $dstfile);

when I run the php I need to copy all the files in the directory

C: \ xampp \ htdocs \ images \ == > C: \ xampp \ htdocs \ images-copied \

Conditions:

  • the destination folder C: \ xampp \ htdocs \ copied-images \ will always be empty
  • The source folder C: \ xampp \ htdocs \ images \ always has images
  • They will never be duplicated because before copying they are deleted with: the function: array_map ('unlink', glob
  • asked by Ivan Diaz Perez 24.05.2018 в 14:09
    source

    1 answer

    3

    The problem with your code is that php does not recognize the *.* as does windows, so what you should do is go through the directory where the images are to be copied and for each file that you find copy it in the new directory.

    $from = 'C:\xampp\htdocs\imagenes';
    $to = 'C:\xampp\htdocs\imagenes-copiadas';
    
    //Abro el directorio que voy a leer
    $dir = opendir($from);
    
    //Recorro el directorio para leer los archivos que tiene
    while(($file = readdir($dir)) !== false){
        //Leo todos los archivos excepto . y ..
        if(strpos($file, '.') !== 0){
            //Copio el archivo manteniendo el mismo nombre en la nueva carpeta
            copy($from.'/'.$file, $to.'/'.$file);
        }
    }
    

    EYE according to the source folder you only have files, if they are inside several folders, you have to modify the code a bit to make it recursive.

    ANOTHER VARIANT would use the windows command to copy files, with this if you can use masks like .

    $from = 'C:\xampp\htdocs\copy\images\*.*';
    $to = 'C:\xampp\htdocs\copy\copy-images';
    //Ejecuto el comando para copiar los archivos de la carpeta from a to
    exec('copy "'.$from.'" "'.$to.'"');
    

    I HOPE THIS CAN RESOLVE YOUR PROBLEM

        
    answered by 24.05.2018 / 14:21
    source